User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,762 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,705 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1125 | Replies: 11
Reply
Join Date: Oct 2006
Posts: 16
Reputation: iubike is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iubike iubike is offline Offline
Newbie Poster

Program works... now I need to create a function, HELP!

  #1  
Dec 12th, 2006
Well the program takes a string of text and then turns it into title case (first letter or every word is capitalized). So I got it working as you see, the problem is I need to write a seperate function that can simply accept the string to be converted as an argument, and convert the string in place. I am not so good at the whole function thing so... any help would be great. As a note... I am 100% lost on this.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. char text[255] ;
  8. int i, first = 1;
  9.  
  10. printf(" Enter String: ");
  11. fgets( text, 255, stdin );
  12.  
  13.  
  14. for ( i = 0; text[i] != '\0'; ++i )
  15. {
  16. if ( first )
  17. {
  18. text[i] = toupper(text[i]);
  19. first = 0;
  20. }
  21. if ( isspace(text[i]) )
  22. {
  23. first = 1;
  24. }
  25. }
  26.  
  27. puts( text );
  28.  
  29. return 0;
  30. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: Bucharest
Posts: 59
Reputation: Eko is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Program works... now I need to create a function, HELP!

  #2  
Dec 12th, 2006
#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
void uppercase(char []);
int main()
{
   char text[255] ;
   
   
   printf(" Enter String: ");
   fgets( text, 255, stdin );
   uppercase(text);
   
   return 0;
}

void uppercase(char text[])
{

int i, first = 1;

for ( i = 0; text[i] != '\0'; ++i )
   {
      if ( first )
      {
         text[i] = toupper(text[i]);
         first = 0;
      }
      if ( isspace(text[i]) )
      {
         first = 1;
      }
   }
   
   puts( text );
   

}

It wasn't a big deal
"Get rich or die tryin"(50-cent)
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,714
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 882
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Program works... now I need to create a function, HELP!

  #3  
Dec 12th, 2006
functions are pretty simple. You have already written one function -- main().
int foo(char text[])
{
  // put your code here
}

int main()
{
 char text[255] ;

<snip>
  foo( text );

<snip>

[edit] Sorry, Eko -- I didn't see your post. [/edit]
Last edited by Ancient Dragon : Dec 12th, 2006 at 11:55 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Posts: 16
Reputation: iubike is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iubike iubike is offline Offline
Newbie Poster

Re: Program works... now I need to create a function, HELP!

  #4  
Dec 12th, 2006
Thanks guys, I really appreciate the help.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Program works... now I need to create a function, HELP!

  #5  
Dec 12th, 2006
>I am not so good at the whole function thing so
Functions are very simple. You can think of them as a named block of code. Let's walk through a few steps of factoring your code into a function. First, take the code you want in a separate function and wrap a block around it:
#include <stdio.h>
#include <ctype.h>
 
int main()
{
  char text[255];
  int i, first = 1;

  printf( "Enter String: " );
  fgets( text, 255, stdin );

  /* This is the blueprint for your function's body */
  {
    for ( i = 0; text[i] != '\0'; ++i )
    {
      if ( first )
      {
        text[i] = toupper(text[i]);
        first = 0;
      }
      if ( isspace( text[i] ) )
      {
        first = 1;
      }
    }
  }
   
  puts( text );
   
  return 0;
}
You need to take note of two things: First, does the block rely on variables created outside of it? Second, does the block just have a side effect or does it actually produce a result? The answer to the first question is yes, the block uses the text array. It also uses i and first. The answer to the second is a little harder. Since the only changes are the ones made to the text array, you can safely say that the block only has a side effect, so we'll go with that to make things easier.

Those two questions determine the parameters, local variables, and return type of your function. Since you use an array defined outside of the block, and you still use it in main, you need to pass it as an array parameter. Since i and first are only used inside of the block, you can move them into the block. They become the block's local variables. Since you don't produce a result and only rely on side effects (the changes to the array), the return type is void, for nothing.

Now for the surgery. Cut the block out of your main, because a function can't be defined inside of another function, and paste it out in limbo at the same level as main:
#include <stdio.h>
#include <ctype.h>

/* This is the blueprint for your function's body */
{
  int i, first = 1;

  for ( i = 0; text[i] != '\0'; ++i )
  {
    if ( first )
    {
      text[i] = toupper(text[i]);
      first = 0;
    }
    if ( isspace( text[i] ) )
    {
      first = 1;
    }
  }
}
 
int main()
{
  char text[255];

  printf( "Enter String: " );
  fgets( text, 255, stdin );

  /* This is where the block once was */
  /* And where you will call the new function */

  puts( text );

  return 0;
}
You're most of the way there now. All you need to do is give the block a name, otherwise how will you know how to call it? The name is easy, just a return type, whatever name you want, and a variable declaration inside parentheses:
#include <stdio.h>
#include <ctype.h>

/* This is the new function */
void title_case ( char text[255] )
{
  int i, first = 1;

  for ( i = 0; text[i] != '\0'; ++i )
  {
    if ( first )
    {
      text[i] = toupper(text[i]);
      first = 0;
    }
    if ( isspace( text[i] ) )
    {
      first = 1;
    }
  }
}
 
int main()
{
  char text[255];

  printf( "Enter String: " );
  fgets( text, 255, stdin );

  /* This is where the block once was */
  /* And where you will call the new function */

  puts( text );

  return 0;
}
We just made a function! Now all that's left to do is call it:
#include <stdio.h>
#include <ctype.h>

/* This is the new function */
void title_case ( char text[255] )
{
  int i, first = 1;

  for ( i = 0; text[i] != '\0'; ++i )
  {
    if ( first )
    {
      text[i] = toupper(text[i]);
      first = 0;
    }
    if ( isspace( text[i] ) )
    {
      first = 1;
    }
  }
}
 
int main()
{
  char text[255];

  printf( "Enter String: " );
  fgets( text, 255, stdin );

  /* This is where the block once was */
  /* And where you call the new function */
  title_case( text );

  puts( text );

  return 0;
}
Piece of cake, right?
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2006
Posts: 16
Reputation: iubike is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iubike iubike is offline Offline
Newbie Poster

Re: Program works... now I need to create a function, HELP!

  #6  
Dec 12th, 2006
Wow Narue... that is great Thank you so much. Now I will try to use that skill to create two functions to convert these temps... should be fun.

  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. struct Temperature
  8. {
  9. float in;
  10. float out;
  11. };
  12. typedef struct Temperature Temperature;
  13.  
  14. int main( int argc, char** argv )
  15. {
  16. char scaleIn = '\0';
  17. int minTemperature;
  18. int maxTemperature;
  19. int deltaTemperature;
  20. int numTemperatures;
  21. char scaleOut;
  22. Temperature* temperatures;
  23. int temperature;
  24. int i;
  25.  
  26. // Get the input temperature scale
  27. while( (scaleIn != 'C') && (scaleIn != 'F') )
  28. {
  29. printf( "Enter a temperature scale (F/C): " );
  30. scanf( " %c", &scaleIn );
  31. getchar(); // read the newline out of the way
  32. scaleIn = toupper( scaleIn );
  33. }
  34.  
  35. // Given the input temperature scale, set the output temperature scale
  36. if( scaleIn == 'F' )
  37. scaleOut = 'C';
  38. else
  39. scaleOut = 'F';
  40.  
  41. // Get the min, max, and interval temperature values
  42. // Put the temperature scale in the prompt as a reminder
  43. printf( "Enter a minimum temperature (%c): ", scaleIn );
  44. scanf( " %d", &minTemperature );
  45. getchar(); // read the newline out of the way
  46.  
  47. printf( "Enter a maximum temperature (%c): ", scaleIn );
  48. scanf( " %d", &maxTemperature );
  49. getchar(); // read the newline out of the way
  50.  
  51. printf( "Enter a temperature interval (%c): ", scaleIn );
  52. scanf( " %d", &deltaTemperature );
  53. getchar(); // read the newline out of the way
  54.  
  55. // Compute the number of temperatures we need to calculate
  56. // numTemperatures = (maxTemperature - minTemperature) / deltaTemperature + 1;
  57. numTemperatures = ceil( (float) (maxTemperature - minTemperature) / deltaTemperature ) + 1;
  58. // numTemperatures = rint( (float) (maxTemperature - minTemperature) / deltaTemperature ) + 1;
  59. // printf( "numTemperatures = %d\n", numTemperatures );
  60.  
  61. // Allocate the temperatures array (safely)
  62. temperatures = (Temperature*) malloc( numTemperatures * sizeof( *temperatures ) );
  63. if( !temperatures )
  64. {
  65. printf( "Failure allocating temperatures (%lu)\n", numTemperatures * sizeof( *temperatures ) );
  66. exit( 1 );
  67. }
  68.  
  69. // Calculate the table's input and output temperatures
  70. temperature = minTemperature;
  71. for( i = 0; i < numTemperatures; i++ )
  72. {
  73. temperatures[i].in = temperature;
  74. temperature += deltaTemperature;
  75.  
  76. if( scaleIn == 'F' )
  77. temperatures[i].out = (temperatures[i].in - 32.0) * 5.0 / 9.0;
  78. else // scaleIn == 'C'
  79. temperatures[i].out = 32.0 + temperatures[i].in * 9.0 / 5.0;
  80. }
  81.  
  82. // Display the table of temperatures
  83. for( i = 0; i < numTemperatures; i++ )
  84. printf( " %3.0f %c = %3.0f %c\n", temperatures[i].in, scaleIn, temperatures[i].out, scaleOut );
  85.  
  86. // Clean up the memory we allocated
  87. free( temperatures );
  88.  
  89. return( 0 );
  90. }
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Program works... now I need to create a function, HELP!

  #7  
Dec 12th, 2006
>should be fun.
It always is. The only difference with this one is now you have a return value and you'll need to change temperatures[i].in to whatever parameter name you choose. In the last one we could still use text because that's what the parameter was called. The name of the parameter and the name of the argument don't have to be the same. By the way, this is a parameter:
void title_case ( char text[255] )
And this is an argument:
title_case( text );
Some people get confused about which is which, but a parameter is the variable that the function uses and an argument is the value of the variable that you pass to the function.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2006
Posts: 16
Reputation: iubike is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iubike iubike is offline Offline
Newbie Poster

Re: Program works... now I need to create a function, HELP!

  #8  
Dec 12th, 2006
Argh... Running into a wall on this one. I assign the value for the temperature[i].in, in the for function, so what exactly do I need to put into the new function, this is what I started, not right though.

  1. void FahrenheitToCelsius( int temperatures[i].in)
  2. {
  3. int i;
  4. temperatures[i].out = (temperatures[i].in - 32.0) * 5.0 / 9.0;
  5. }
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Program works... now I need to create a function, HELP!

  #9  
Dec 12th, 2006
Remember how the parameters and arguments don't have to have the same name? This is a case where they literally cannot. So you pass temperatures[i].in and the parameter would be named something else, like temp, and have a type that matches what you pass.

Also, since your function returns a value, or at least, that makes the most sense in this case, the return type would match the type you're assigning to and you use the return statement inside the function:
float FahrenheitToCelsius( float temp )
{
  return (temp - 32.0) * 5.0 / 9.0;
}
Then you would call it like this:
temperatures[i].out = FahrenheitToCelsius( temperatures[i].in );
It's all very consistent, but it takes some time to see the patterns. Don't let frustration get the best of you.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2006
Posts: 16
Reputation: iubike is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iubike iubike is offline Offline
Newbie Poster

Re: Program works... now I need to create a function, HELP!

  #10  
Dec 12th, 2006
So would I add a variable in each function that states temp=temperatures[i].in? Sorry for not catching on quicker here .
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 8:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC