Capitalizing first letter of Every word in a string Without Loops

Hi all,

First post here!
And I have registered because I have literally spent the last 6 hours searching High & Low, all over this site, every search term I could think of, and all over google, to find this answer to No avail.

I need to capitalize the first letter of every word in a string, without using any loops in the capitalization process.
Specifically I am going to use this to retrieve the strings, stored in an array, and lowercase every letter in each string, then uppercase the first letter of every word.

I am currently using standard library functions (?) to take care of the lowercasing without using any loops, but cannot figure out how to capitalize the first letter in every word thereafter without using loops.

Here's what I have so far for lowercasing:

1
2
3
4
string s=*whatever goes here*;

int (*tl)(int) = tolower; // Selecting this particular overload
transform(s.begin(),s.end(),s.begin(),tl );


and this is what I have for uppercasing Every letter in the string, after they have all been lowercased:

1
2
3
int (*tu)(int) = toupper;
transform(str.begin(), str.end(), str.begin(), tu));
cout << str << std::endl;


My main issue is, telling a function to uppercase Only the first letter of every word in a string (that contains a sentence).


Maybe something from the string member function can be used?
Or something with c-string, buffer, fill, or range for a string? I don't really know what those are (I'm a programming n00b in my first class), but they seem like they may allow for differentiating between areas in a string.

Maybe using string::find_first_of, string::find, or string::replace functions?
Last edited on
i'm pretty sure there is a built in function called "toupper" that does something like that but I haven't read about how to use it.

This is one solution though. You subtract the offset of capital A from lower case a then add it to the letter.

each letter corresponds to an ascii number (i believe) so when you add like this it basically increases it to the number corresponding to the capital letter.

Not sure if that's worded right, i'm a noob also but this seems to work here.

1
2
3
4
5
6
7
8
9
10
// upper case
#include <iostream>

int main ()
{
  char a = 'x';
  a = a + ('A' - 'a');
  std::cout << a;
  return 0;
}
Thanks for the Reply Garion!

Interesting... had no idea you could capitalize that way!
that's cool

I still don't know how to set it so it does that to Only the first letter of every word in a string through...
ahh, this seems to work also.

I'm not 100% sure but I believe a string is in essence a "character array"

so this seems to work here.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <fstream>

int main()
{

    std::string myWord = "hoobily";
    myWord[0] = myWord[0] + ('A' - 'a');
    std::cout << myWord;
    return 0;
}
Oh Good call Garion! Forgot about that one.
That definitely capitalized the first letter of the word!

Any idea how to split up a string (maybe into temporarily individual strings such as your 'myWord' above?) such as:

string s = "this is the sentence that should be all titlecased."

to be:

//function does something to 's'

s outputs -> This Is The Sentence That Should Be All Titlecased.

?


I have this array of strings that are sentences like above, and need to do this individually to each string, then re-insert these titlecased strings back into the same values of the same array (so if we split the sentences into individual string words for titlecasing, they would have to be put back together in the same sentences).

Maybe a function that does this uppercasing to a string after every white space?
I'm still working on learning that myself. I've been trying to learn how to parse a text file but I haven't quite mastered this stuff.

I have some ideas but I have no idea if they are correct. My brain is already tired today but I'll try some more tomorrow. If i figure it out I'll let you know.

reading about tokens and delimeters sounded like what would be needed for that but I think you might be able to do an array of pointers too. no idea if either works yet though.
Gotcha. Haha mine is getting pretty tired too, but I am determined to figure this out before it shuts down.
I'll look into those.

Thanks again Garion; hope you get some good rest.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

void transform_if_first_of_word( char& c )
{
    // if the previous character was a space, transform it toupper
    if( (*(&c - sizeof(char))) == ' ')
        c = toupper( c );
}

int main()
{
   std::string str = "the Quick brown fox jumps over the lazy dog";
   
    str[ 0 ] = toupper( str[ 0 ]);
        
   std::for_each( str.begin()+1, str.end(), transform_if_first_of_word );
   // http://www.cplusplus.com/reference/algorithm/for_each/
   
    std::cout << str;
   
   return 0;
}


The Quick Brown Fox Jumps Over The Lazy Dog
Last edited on
Topic archived. No new replies allowed.