Converting a string of words into correct cases

This is a discussion on Converting a string of words into correct cases within the C++ Programming forums, part of the General Programming Boards category; Let's say I have a string s: Code: string s = "How are YOU doING toDay?" I want to convert ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    39

    Converting a string of words into correct cases

    Let's say I have a string s:
    Code:
    string s = "How are YOU doING toDay?"
    I want to convert this string so that only the first letter of each word is capitalized (so I want it to be: "How Are You Doing Today?"). I was thinking that it would be easier to first convert each letter to lowercase (using a string iterator and the tolower() function), and then capitalize the first letter of each word. But this is turning out to be more difficult than I thought. If it was just two words then I was thinking I can use substr() and find_first_of() to separate the words and capitalize each part, but the thing is I'm not guaranteed the number of words in the string. Any ideas are appreciated!

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    capitalize the first letter in the string
    loop through the string to find spaces
    capitalize the letter after each space

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by h_howee View Post
    capitalize the first letter in the string
    loop through the string to find spaces
    capitalize the letter after each space
    ...and convert everything else to lowercase.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. string converting upper/lower
    By jlamn in forum C Programming
    Replies: 9
    Last Post: 09-24-2002, 07:01 PM

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