February 17th, 2012, 08:09 PM
[RESOLVED] Capitalize a String in C++
I have a problem. I need to print the string called "last" in uppercase format. can you check why my program prints nothing.
here is the important section of the code.
if (infile.is_open()) // if file was able to open
while (getline(infile, line))
string::size_type pos1 = line.find(' '); //pos1 is the position of the first space
if (pos1 != string::npos)
string::size_type pos2 = line.find(',', pos1); //pos2 is the position of the comma
string last = line.substr(pos1, pos2 - pos1); //remove everything before the first space
const int length = last.length();
for(int i=0; i!=length ; ++i)
last[i] = std::toupper(last[i]);
cout <<last <<endl;
outfile << last << endl;
February 18th, 2012, 12:39 AM
Re: Capitalize a String in C++
Not really. You've only given a small part of your code and it's hard to say what's really going on, especially since this code is working with file IO.
Why don't you try using your debugger and step through it to see what is happening.
Also, you could simply use this to capitalize a string.
std::transform(last.begin(), last.end(), last.begin(), std::toupper);
Tags for this Thread
Click Here to Expand Forum to Full Width