Thread: capitalizing only first letter in strings??

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    capitalizing only first letter in strings??

    I am trying to take a users input and then use a loop to loop through each individual character in the string the user has inputted. Then while it loops through it capitalizes the first letter only leaving the others lowercase unless it reaches a space before a character then I want to capitalize the character following the space.

    For ex.

    Someone enters in north dakota

    it loops through, capitalizes the n. keeps looping through each char until it reaches the space, then checks for a character after that and if it finds a character after that it will capitalize it

    so the end result is: North Dakota.

    But at the same time I want it to lower case it if a user enters in all caps like

    NORTH DAKOTA

    but leave the first letters capitalized.

    Is the toupper() function a proper one to use here? Basically loop through capitalizing the first letter regardless and also if the character follows a space while keeping the rest lowercase.

    if someone can guide me in the right direction i'd appreciate it . . thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, include <ctype.h> and use string[i] = toupper(string[i]), right after a space or on the first char in the string. All the other char's in the string can get hit with tolower(string[i]), using the same kind of logic.

    You don't have to worry about any "leave" logic. Capitalize the char's you need to (regardless of what they are now), or set them to lower case, explicitly, following your logic.

    Their current case is irrelevant.
    Last edited by Adak; 03-25-2011 at 10:15 AM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Create a function "capitalize(char *str)" and use a boolean flag to mark if you are in a word or outside a word. If word == false, call toupper() and set word to true. If *str == ' ' set word to false, else call tolower().

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    well here is what I have so far
    Code:
                   void strCase(char s[])
                   {
                        int i;
                        while (s[i] != NULL)
                        {
                              i = toupper(s[i]);
                              i++;
                        }
                   }
    I am just testing to see if this will capitalize all letters , getting an error right now but am I on the right track?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Using i as the index should assign it to s[i]. So instead of "i = toupper(s[i]);" try: "s[i] = toupper(s[i]);"

    This should make everything uppercase. You need some if statements in there looking for (s[i] == ' ') and if this is true, take appropriate action.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    fixed thx
    Last edited by cprogrammer1980; 03-25-2011 at 11:53 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Simple nasty trick...

    1) convert the entire string to lower case in a loop.
    2) Find the spaces and convert the character after to upper case.

    Nice an easy....

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by CommonTater View Post
    Simple nasty trick...

    1) convert the entire string to lower case in a loop.
    2) Find the spaces and convert the character after to upper case.

    Nice an easy....
    should I do this using an 'if' statement?

    Code:
       int strCase(char s[])
                   {
                        int i;
                         while (s[i] != '\0')
                        {
                              s[i] = toupper(s[i]);
                              i++;
                              }
                        return 0;
                   }
    this is what I have so far I am toying around with making it all CAPS, the code compiles but it isnt converting them for some reason
    Last edited by cprogrammer1980; 03-25-2011 at 11:55 AM.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The function you have there should work to capitalize the entire string. You need to keep track if your index is a blank, you can test this with: if( s[i] == ' ' )

    And if you think about it, you should always call tolower(), except if your character is the first after a space.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cprogrammer1980 View Post
    should I do this using an 'if' statement?

    Code:
    int strCase(char s[])
                   {
                        int i=0;
                        while (s[i])
                        {
                              s[i] = toupper(s[i]);
                              i++;
                              }
                        return 0;
                   }
    this is what I have so far I am toying around with making it all CAPS, the code compiles but it isnt converting them for some reason
    This is UNTESTED code. It's only to show concept...
    Code:
    void strcase(char *str)
      { int x, y;  // loop variables
         char *ucp;  // upper case pointer 
         y = strlen(str);
         // convert to all lower case
         for( x = 0;  x < y; x++)
           {  str[x] = tolower(str[x]); }
        // capitalize words
        ucp = str;
        while (ucp = strchr(ucp,' '))
           { ++ ucp;
               *ucp = toupper(*ucp);}  }
    Of course this won't work if there are multiple spaces between words... but removing them is a whole other problem.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Alternatively you can use flags as I mentioned earlier, this is the basic idea of that.

    Code:
    void capitalize(char *str) {
        bool word = false;
        char *end = str + strlen(str);
        for(; str < end; str++){
            if( *str == ' ' ){
                word = false;
            } else if( word == false ) {
                *str = toupper(*str);
                word = true;
            }
            else {
                *str = tolower(*str);
            }
        }
    }

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I see, It doesn't say anything about more than one space so that is fine but
    question as to why you made strcase void?

    Does it automatically do the conversions despite not being called anywhere else in the code or will I have to call strcase() somewhere in my code?

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to call it yourself, after that it will change the string in-place, no need to return anything.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cprogrammer1980 View Post
    I see, It doesn't say anything about more than one space so that is fine but
    question as to why you made strcase void?
    Because it doesn't return anything.

    Does it automatically do the conversions despite not being called anywhere else in the code or will I have to call strcase() somewhere in my code?
    You're kidding right?
    No function does anything without being called...

    You call it as...
    Code:
    strcase(StringToBeFancyCapped);

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I see, I have one function that takes in the input from the user a second function that re-displays the user input so my guess would be to call it in that second function so it re-displays the input starting with a capital letter.

    Also in the code you posted, what is the difference between char *str and char s[] because I am passing in char s[] from the input function and I am not familiar with what the asterisk stands for. Sorry I am still a bit of a noob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. identifying first letter of word
    By speedyboy in forum C Programming
    Replies: 11
    Last Post: 03-19-2006, 07:56 AM
  5. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM