int to char conversion

Hi, I need to store the value of an int variable as an element in a string. i.e.

1
2
3
4
string S = "abcde";
int count;
count = 15; //i.e count gets assigned some value during the course of the program
S[3] = count; //<= problem! I want to store count as an i'th element of the string but the problem is that count is an int and I don't know its value so can't use '15' 



I want S to become "abc15e". How could I do this?
EDIT: Moschops beat me, but I want to point out that from what you said you want to replace and not insert.

Start by converting the int to a string. This is most easily done with a stringstream:
1
2
3
ostringstream ss;
ss << count;
string temp = ss.str();

http://www.cplusplus.com/reference/iostream/ostringstream/

Then, you can replace the third character:
S.replace(3, 1, temp);
http://www.cplusplus.com/reference/string/string/replace/
Last edited on
Thanks Moschops and L B.

L B is right that I wanted to replace rather than insert. I didn't get time to work on the problem since posting this on March 2. Got it working today based on your inputs. :)
Topic archived. No new replies allowed.