capitalize a string

Hi, I need help with this.
I have to create a function that takes an input string and changes it to all capital letters

This is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

void capitalize(string& name2) {
    for (short i = 0; i < name2.size(); ++i)
    name2 += toupper(name2[i]);
}

int main() {
    string name2 = "brenda";
    capitalize(name2);
    
    cout << name2 << end;
}


it works but the output instead of being "BRENDA" is "brendaBRENDABRENDABRENDABRENDABRENDABRENDABRENDABRENDABRENDABRENDABRENDA...etc"

please help
Last edited on
name2 += toupper(name2[i]); You are appending it to the end. Shouldn't this be name2[i] = toupper(name2[i]); PS when you append you increment the size so you have an infinite loop as mentioned by LB
Last edited on
http://stackoverflow.com/a/313990/1959975

I don't understand how your code even got out of that infinite loop.
Last edited on
Might have to do with appending an int to a std::string. Though, that would just cast it to a char so I have no idea actually.
Last edited on
giblit, i tried using name2[i] = toupper(name2[i]) but it gave some strange numbers.

I changed the function to a returning one, but still gives me the string un uppercase

1
2
3
4
5
6
7
8
9
10
11
12
13
string capitalize(string name2) {
    string upStr;
    for (short i = 0; i < name2.size(); i++) {
        upStr += toupper(name2[i]);
    }
    return upStr;

int main () {
  string name2 = "brenda";
    capitalize(name2);
    
    cout << name2 << endl << endl << endl;
}


unless I change the output to cout << capitalize(name2) << end; it works, but I have to use the other one
The second code probably has undefined output since upStr is undefined. The first will work fine or initalize upStr to an empty string. Maybe you hit run and didn't compile first because end should be endl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

void capitalize(string& name2) {
    for (short i = 0; i < name2.size(); ++i)
    name2[i] = toupper(name2[i]);
}

int main() {
    string name2 = "brenda";
    capitalize(name2);
    
    cout << name2 << endl;
}
@giblit: string upStr; initializes an empty string, it is not undefined.
@giblit: this is what I get
\244Ī\274\250\242
using
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

void capitalize(string& name2) {
    for (short i = 0; i < name2.size(); ++i)
    name2[i] = toupper(name2[i]);
}

int main() {
    string name2 = "brenda";
    capitalize(name2);
    
    cout << name2 << endl;
}
Last edited on
Did you compile it first? You should get "BRENDA"
I'm practically new at c++, I'm using Xcode to build the program and to be honest I don't know how to compile from there.
There should be something like"build and run" instead of "run."
ok, then yes, haha. Although I build it a run I still get the same, I changed it to cout << capitalize(name2) << end; so it can show "BRENDA"
Are you sure you ran giblit's code? http://ideone.com/rvl2dI outputs "BRENDA".
Topic archived. No new replies allowed.