replace/

i have a string.
for example
string s="!!!!!"

if i want to make it like this:"!!s!!" i mean to replace the third ! with the letter s,what will use?

replace,insert,assing?
and how?i will put the first and the last position?
can show me plz?
s[2]='s';
it tell me error,man,i that string to char i cant corvet.
Something else?
i am creating a hangman.

so i have two strings. the guess
and the word


so what to put?

if the word is:_ _ _ _
and i wanna become _ _ c _

how wll replace the _ with the c?
Like I said:
s[2]='c';

If that doesn't work, you're doing something wrong.
To find out what "something" is, post your code.
Man i dont want to replace with a character.

I wanna replace this with a string.
so i put:s[2]=guess;
i cant,so how can make it?
You said you wanted to replace it with a character. Choose one.
If you want to replace a substring with another one, use replace():
http://www.cplusplus.com/reference/string/string/replace/
yea man i know that must use replace.
but if how i use it?(i dont know english ,can show me?)

for example
letter.replace(<from where to start? >,<where to finsh?>,<and here my string?>)
For example:
s.replace(2,1,"yourString");
yea man i know but because i have some problems with it
the first number is the start and the other the end?

for example if guess="!"
and word="_ _ _ _"

how will do it to be
word="_ _ ! _"

?
Just look at the reference, you see all the available variants, no English required.
The one used in my example was this one:
string& replace ( size_t pos1, size_t n1, const char* s );

The first argument (2) is the start index of the string to be replaced, the second one (1) is its length and the last one is the string you want to replace it with ("yourString").

If you use iterators, you can specify the beginning and end instead:
s.replace(s.begin()+2,s.begin()+3,"yourString");
This does exactly the same as the first example.
Last edited on
Thank you man,i found it.

i just thought that the second number is where finish the place where i want to replace,but now i understood that it i was the lenght(size) of the string which i will put.

:P
Topic archived. No new replies allowed.