How can I pass a string to a function??
Printable View
How can I pass a string to a function??
void myfun( char * stringy );
...
char buf[BUFSIZ];
myfun( buf );
Quzah.
I suppose the real answer is you can't, you have to pass a pointer to the string, like in the example given.Quote:
Originally posted by Paninaro
How can I pass a string to a function??
A simple program to check whether the first or last characters of a string are uppercase...Code:#include <ctype.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int StringStartsWithUpper (char *);
int StringEndsWithUpper (char *);
char * myString = "This is the sample string";
int main (void) {
printf ("%s\n", myString); // Pass the string to printf
if (StringStartsWithUpper (myString)) { // Pass the string to this function
printf ("Starts with an upper case letter.\n");
} else {
printf ("Doesn't start with an upper case letter.\n");
}
if (StringEndsWithUpper (myString)) { // Pass the string to function
printf ("Ends with an upper case letter.\n");
} else {
printf ("Doesn't end with an upper case letter.\n");
}
return 0;
}
int StringStartsWithUpper (char * checkMe) {
return isupper(checkMe[0]); // Pass a char to isUpper()
}
int StringEndsWithUpper (char * checkMe) {
int length = strlen (checkMe); // Pass the string to strlen. Returns the length of the string.
return isupper (checkMe [length - 1]); // Pass a char to isUpper()
}