Is there a standard C function to do this?

This is a discussion on Is there a standard C function to do this? within the C Programming forums, part of the General Programming Boards category; I wrote some code today to accept user input for first and last names and then make sure it's formated ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Is there a standard C function to do this?

    I wrote some code today to accept user input for first and last names and then make sure it's formated as such: Randy Collins

    So if the user types RaNDy or rANdY or randy it will format it and then output. My program works fine, but I was wondering if there was a Standard C function I haven't found yet that will do this much easier/faster?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int tolower( int c );
    int toupper( int c );
    
    int main() {
    
    char FirstName[21];
    char LastName[21];
    int i, x;
    
    	printf( "Please enter your FIRST name: " );
    	scanf( "%[^\n]%*c", FirstName );
    
    	printf( "Please enter your LAST name: " );
    	scanf( "%[^\n]%*c", LastName );
    
    		printf( "\nYour full name is: " )
    
    			for ( i = 0 ; FirstName[i] != 0 ; i = i + 1 ) { 
    			
    				for ( x = 0; x < 1; x = x + 1 ) {
    			                printf( "%c", toupper( FirstName[i] ) );
    				i = 1;
    				}
    
    			printf( "%c", tolower( FirstName[i] ) );
    			}
    
    
    			printf(" ");
    
    
    			for ( i = 0 ; LastName[i] != 0 ; i = i + 1 ) {
    
    				for ( x = 0; x < 1; x = x + 1 ) {
    			                printf( "%c", toupper( LastName[i] ) );	
    				i = 1;
    				}
     
    			printf( "%c", tolower( LastName[i] ) );
    			}
    
    return 0;
    }
    Last edited by lucidrave; 08-14-2009 at 10:14 AM. Reason: misc

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, there's no standard function that does that.

    (One comment: There's no reason to do a for-loop from zero to zero. Just call toupper(FirstName[0]).)

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    Thanks.

    Yeah, I guess I was just overthinking it a bit with that for-loop for x. I'll modify the code

    Quote Originally Posted by tabstop View Post
    No, there's no standard function that does that.

    (One comment: There's no reason to do a for-loop from zero to zero. Just call toupper(FirstName[0]).)

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What you could do is create your own function that performs the case changing you want, and then you won't have duplicated logic in your program.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    char* capitalize( char* str )
    {
    	int 
    		cap = 1;
    	char* 
    		ptr = str;
    	for( ; *ptr; ++ptr )
    	{
    		if( !isalpha( *ptr ) )
    			cap = 1;
    		else if( cap )
    			*ptr = toupper( *ptr ), 
    			cap = 0;
    		else
    			*ptr = tolower( *ptr );
    	}	
    	return str;
    }
    hth.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I was thinking something a bit more compact.
    Code:
    void capitalize( char* str ) {
    	
    	int i = 1 ; 
    	if (! *str) return ;   // emtpy string 
    	str[0] = toupper(str[0]) ; 
    	while (str[i] ) { 
    		str[i++] = tolower(str[i]) ; 
    	}
    	return ; 
    }
    Last edited by Dino; 08-14-2009 at 01:11 PM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Dino View Post
    I was thinking something a bit more compact.
    Code:
    void capitalize( char* str ) {
        
        int i = 1 ; 
        if (! *str) return ;   // emtpy string 
        str[0] = toupper(str[0]) ; 
        while (str[i] ) { 
            str[i++] = tolower(str[i]) ; 
        }
        return ; 
    }
    That works too (the empty string check isn't necessary, though), but of course the string couldn't have any leading whitespace. What I posted actually capitalizes all of the "words" in the text, which may not be desirable in some cases (eg: just capitalizing the first word in a sentence), but for the general case, it's a pretty useful approach. For instance, let's say the input is "615 texas ave." - the function would convert that to "615 Texas Ave.", etc.

    EDIT:
    Whoops, the empty string check is necessary, actually. Sorry.
    Last edited by Sebastiani; 08-14-2009 at 02:31 PM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Here's my 2c:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void capitalize(char *name)
    {
        for (int i = 0 ; name[i]; ++i)
            printf("%c", i ? tolower(name[i]) : toupper(name[i]));
    }
    
    int main()
    {
        char FirstName[21];
        char LastName[21];
        int i, x;
    
        printf( "Please enter your FIRST name: " );
        scanf( "%[^\n]%*c", FirstName );
        printf( "Please enter your LAST name: " );
        scanf( "%[^\n]%*c", LastName );
    
        printf( "\nYour full name is: " );
        capitalize(FirstName);
        printf(" ");
        capitalize(LastName);
        printf("\n");
    }

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    I like your version of code as it seems the most readable to me... However, there are a few parts of one line of code that I don't quite understand. Could you or someone explain this to me?

    I'm not sure about the the i ? and :

    Code:
    printf("%c", i ? tolower(name[i]) : toupper(name[i]));

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's the ternary operator - basically shorthand for if/else expressions:

    Code:
    char ch;
    if(i)
        ch = tolower(name[i]);
    else
        ch = toupper(name[i]);
    printf("%c", ch);
    They can be nested as well. For instance:

    Code:
    printf("%c", i > 0 ? tolower(name[i]) : i == 0 ? toupper(name[i]) : '?');
    // same as:
    char ch;
    if(i > 0)
        ch = tolower(name[i]);
    else if(ch == 0)
        ch = toupper(name[i]);
    else /* i < 0 */
        ch = '?';
    printf("%c", ch);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

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