I have some trouble in beginning

I want to convert capital letters to lower case letters by "tolower();".
My code:
1
2
3
4
5
6
7
 
string text;
string text_lower;
for (int i=0;i<=text.length(); i++) {
        text_lower[i] = std::tolower(text[i]);
            }
cout<<text_lower;


After I compiled code, when I run it.
It cannot print the lower case letters, it also prints nothing .
And when I change the code.
1
2
3
4
5
6
7
8
 
string text;
string text_lower;
for (int i=0;i<=text.length(); i++) {
        text_lower[i] = std::tolower(text[i]);
        cout<<text_lower[i];    //This can print the lower case letters
        cout<<std::tolower(text[i]);  //This print the ASCII.
}
try this:
1
2
3
4
5
string text;
string text_lower = text;
for (int i=0;i < text.length(); ++i)
        tolower(text_lower[i]);
cout<<text_lower;
Last edited on
Thank you.
But Can you tell me why?
your code:
text_lower[i] = std::tolower(text[i]);

this does not work coz you are indexing an uninitialized string.
in your declaration:
string text_lower;
text_lower = "" so you can't indexing it.

when for loop finis it's job text_lower will still be "" (empty string)
Topic archived. No new replies allowed.