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.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
   char text[255] ;
   int i, first = 1;
   
   printf(" Enter String: ");
   fgets( text, 255, stdin );
   
  
   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;
}
#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 ;)

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]

Thanks guys, I really appreciate the help.

>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? :)

commented: Nicely done - Salem +4
commented: A very patiently explained answer +1

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.

#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

struct Temperature
{
    float    in;
    float    out;
};
typedef  struct Temperature  Temperature;

int main( int argc, char** argv )
{
    char            scaleIn = '\0';
    int                minTemperature;
    int                maxTemperature;
    int                deltaTemperature;
    int                numTemperatures;
    char            scaleOut;
    Temperature*    temperatures;
    int                temperature;
    int                i;
    
    // Get the input temperature scale
    while( (scaleIn != 'C') && (scaleIn != 'F') )
    {
        printf( "Enter a temperature scale  (F/C): " );
        scanf( " %c", &scaleIn );
        getchar();    // read the newline out of the way
        scaleIn = toupper( scaleIn );
    }
    
    // Given the input temperature scale, set the output temperature scale
    if( scaleIn == 'F' )
        scaleOut = 'C';
    else
        scaleOut = 'F';
    
    // Get the min, max, and interval temperature values
    // Put the temperature scale in the prompt as a reminder
    printf( "Enter a minimum temperature  (%c): ", scaleIn );
    scanf( " %d", &minTemperature );
    getchar();    // read the newline out of the way
    
    printf( "Enter a maximum temperature  (%c): ", scaleIn );
    scanf( " %d", &maxTemperature );
    getchar();    // read the newline out of the way
    
    printf( "Enter a temperature interval (%c): ", scaleIn );
    scanf( " %d", &deltaTemperature );
    getchar();    // read the newline out of the way
    
    // Compute the number of temperatures we need to calculate
//    numTemperatures = (maxTemperature - minTemperature) / deltaTemperature  +  1;
    numTemperatures = ceil( (float) (maxTemperature - minTemperature) / deltaTemperature )    +  1;
//    numTemperatures = rint( (float) (maxTemperature - minTemperature) / deltaTemperature )  +  1;
//    printf( "numTemperatures = %d\n", numTemperatures );

    // Allocate the temperatures array (safely)
    temperatures = (Temperature*) malloc( numTemperatures * sizeof( *temperatures ) );
    if( !temperatures )
    {
        printf( "Failure allocating temperatures (%lu)\n", numTemperatures * sizeof( *temperatures ) );
        exit( 1 );
    }

    // Calculate the table's input and output temperatures
    temperature = minTemperature;
    for( i = 0; i < numTemperatures; i++ )
    {
        temperatures[i].in = temperature;
        temperature += deltaTemperature;
        
        if( scaleIn == 'F' )
            temperatures[i].out = (temperatures[i].in - 32.0) * 5.0 / 9.0;
        else    // scaleIn == 'C'
            temperatures[i].out = 32.0  +  temperatures[i].in * 9.0 / 5.0;
    }
    
    // Display the table of temperatures
    for( i = 0; i < numTemperatures; i++ )
        printf( "  %3.0f %c  =  %3.0f %c\n", temperatures[i].in, scaleIn, temperatures[i].out, scaleOut );

    // Clean up the memory we allocated
    free( temperatures );
    
    return( 0 );
}

>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.

Argh... Running into a wall on this one. I assign the value for the temperature.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.

void FahrenheitToCelsius( int temperatures[i].in)
{
int i;
            temperatures[i].out = (temperatures[i].in - 32.0) * 5.0 / 9.0;
}

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.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. :)

So would I add a variable in each function that states temp=temperatures.in? Sorry for not catching on quicker here :).

You are awesome! Got it.

#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>



// prototype - define the function

float FahrenheitToCelsius( float temp );        
float CelsiusToFahrenheit( float temp );

struct Temperature
{
    float    in;
    float    out;
};
typedef  struct Temperature  Temperature;

int main( int argc, char** argv )
{
    char            scaleIn = '\0';
    int                minTemperature;
    int                maxTemperature;
    int                deltaTemperature;
    int                numTemperatures;
    char            scaleOut;
    Temperature*    temperatures;
    int                temperature;
    int                i;
    
    // Get the input temperature scale
    while( (scaleIn != 'C') && (scaleIn != 'F') )
    {
        printf( "Enter a temperature scale  (F/C): " );
        scanf( " %c", &scaleIn );
        getchar();    // read the newline out of the way
        scaleIn = toupper( scaleIn );
    }
    
    // Given the input temperature scale, set the output temperature scale
    if( scaleIn == 'F' )
        scaleOut = 'C';
    else
        scaleOut = 'F';
    
    // Get the min, max, and interval temperature values
    // Put the temperature scale in the prompt as a reminder
    printf( "Enter a minimum temperature  (%c): ", scaleIn );
    scanf( " %d", &minTemperature );
    getchar();    // read the newline out of the way
    
    printf( "Enter a maximum temperature  (%c): ", scaleIn );
    scanf( " %d", &maxTemperature );
    getchar();    // read the newline out of the way
    
    printf( "Enter a temperature interval (%c): ", scaleIn );
    scanf( " %d", &deltaTemperature );
    getchar();    // read the newline out of the way
    
    // Compute the number of temperatures we need to calculate
//    numTemperatures = (maxTemperature - minTemperature) / deltaTemperature  +  1;
    numTemperatures = ceil( (float) (maxTemperature - minTemperature) / deltaTemperature )    +  1;
//    numTemperatures = rint( (float) (maxTemperature - minTemperature) / deltaTemperature )  +  1;
//    printf( "numTemperatures = %d\n", numTemperatures );

    // Allocate the temperatures array (safely)
    temperatures = (Temperature*) malloc( numTemperatures * sizeof( *temperatures ) );
    if( !temperatures )
    {
        printf( "Failure allocating temperatures (%u)\n", numTemperatures * sizeof( *temperatures ) );
        exit( 1 );
    }

    // Calculate the table's input and output temperatures
    temperature = minTemperature;
    for( i = 0; i < numTemperatures; i++ )
    {
        temperatures[i].in = temperature;
        temperature += deltaTemperature;
        
        if( scaleIn == 'F' )
            temperatures[i].out = FahrenheitToCelsius( temperatures[i].in );
        else    // scaleIn == 'C'
            temperatures[i].out = CelsiusToFahrenheit( temperatures[i].in );

    }
    
    // Display the table of temperatures
    for( i = 0; i < numTemperatures; i++ )
        printf( "  %3.0f %c  =  %3.0f %c\n", temperatures[i].in, scaleIn, temperatures[i].out, scaleOut );

    // Clean up the memory we allocated
    free( temperatures );
    
    return( 0 );
}


float FahrenheitToCelsius( float temp )
    {
        return (temp - 32.0) * 5.0 / 9.0;
    }

float CelsiusToFahrenheit( float temp )
    {
        return 32.0  +  temp * 9.0 / 5.0;
    }

>So would I add a variable in each function that states temp=temperatures.in?
You already have the variable. It's the parameter. Let's look at your first program with a function:

#include <stdio.h>
#include <ctype.h>

void my_function( char param[] )
{
  int i, first = 1;

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

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

  my_function( text );
   
  puts( text );
   
  return 0;
}

The effect of calling my_function is like this:

#include <stdio.h>
#include <ctype.h>

int main()
{
  char text[255];

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

  {
    /* Copy the value of the argument (text) to the parameter (param) */
    char *param = text;

    /* The body of the function starts here */
    int i, first = 1;

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

  puts( text );

  return 0;
}

A function is logically nothing more than a blueprint for a block of code. The parameters are placeholders for the actual arguments that you pass when you call it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.