String

This is a discussion on String within the C Programming forums, part of the General Programming Boards category; How can I pass a string to a function??...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    String

    How can I pass a string to a function??
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void myfun( char * stringy );

    ...

    char buf[BUFSIZ];

    myfun( buf );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: String

    Originally posted by Paninaro
    How can I pass a string to a function??
    I suppose the real answer is you can't, you have to pass a pointer to the string, like in the example given.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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()
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21