Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got problem converting string to upper case letters

Tags:

c++

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;
}
like image 730
lycon Avatar asked Sep 11 '26 10:09

lycon


1 Answers

toupper will return the uppercase value instead of modifying the value in-place. As such your code should read:

word[j] = toupper(word[j]);
like image 142
fbrereto Avatar answered Sep 12 '26 23:09

fbrereto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!