•
•
•
•
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
![]() |
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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.
c Syntax (Toggle Plain Text)
#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
"Get rich or die tryin"(50-cent)
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,714
Reputation:
Rep Power: 36
Solved Threads: 882
functions are pretty simple. You have already written one function -- main().
[edit] Sorry, Eko -- I didn't see your post. [/edit]
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
>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:
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:
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:
We just made a function! Now all that's left to do is call it:
Piece of cake, right?
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;
}
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;
}
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;
}#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;
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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.
Thank you so much. Now I will try to use that skill to create two functions to convert these temps... should be fun. c Syntax (Toggle Plain Text)
#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
And this is an argument:
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.
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] )title_case( text ); I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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.
c Syntax (Toggle Plain Text)
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[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:
Then you would call it like this:
It's all very consistent, but it takes some time to see the patterns. Don't let frustration get the best of you.
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;
}temperatures[i].out = FahrenheitToCelsius( temperatures[i].in );
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Why program works in Dev-C++ and not in VC++ 2005? (C++)
- For some reason program installations cannot create new start menu shortcuts... (Windows NT / 2000 / XP / 2003)
- C++ homework (C++)
- Create Function that prints letters, words and sentences (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C Forum
- Previous Thread: if statement didnt work as expected
- Next Thread: question



Linear Mode