Using following console application i am converting each string to uppercase letters. But string value in output remains unchanged. what I am doing wrong here. Also any help on doing this efficiently would be appreciated.Thanks for your help.
int main()
{
vector<string> svec, svec_out;
string word;
int run;
cout << "Press 0 to quit giving input string" << endl;
while(1)
{
cin >> word;
svec.push_back(word);
cin >> run;
if (!run)
break;
}
cout << "converting to upper case... " << endl;
int i;
for (i = 0; i!=svec.size(); ++i)
{
word = svec[i];
for (string::size_type j=0; j < word.size(); ++j)
{
toupper(word[j]);
}
svec_out.push_back(word);
}
for ( i = 0; i<svec_out.size(); i++)
cout << svec_out[i] << endl;
return 0;
}
toupper will return the uppercase value instead of modifying the value in-place. As such your code should read:
word[j] = toupper(word[j]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With