capitalize a string

Sep 3, 2014 at 1:46am
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 Sep 3, 2014 at 1:51am
Sep 3, 2014 at 1:57am
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 Sep 3, 2014 at 7:52pm
Sep 3, 2014 at 1:58am
http://stackoverflow.com/a/313990/1959975

I don't understand how your code even got out of that infinite loop.
Last edited on Sep 3, 2014 at 1:58am
Sep 3, 2014 at 1:59am
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 Sep 3, 2014 at 7:51pm
Sep 3, 2014 at 2:13am
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
Sep 3, 2014 at 2:31am
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;
}
Sep 3, 2014 at 2:47am
@giblit: string upStr; initializes an empty string, it is not undefined.
Sep 3, 2014 at 3:22am
@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 Sep 3, 2014 at 3:23am
Sep 3, 2014 at 3:33am
Did you compile it first? You should get "BRENDA"
Sep 3, 2014 at 3:56am
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.
Sep 3, 2014 at 4:02am
There should be something like"build and run" instead of "run."
Sep 3, 2014 at 4:29am
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"
Sep 3, 2014 at 4:32am
Are you sure you ran giblit's code? http://ideone.com/rvl2dI outputs "BRENDA".
Topic archived. No new replies allowed.