replace/

Jul 20, 2012 at 10:42am
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?
Jul 20, 2012 at 10:51am
s[2]='s';
Jul 20, 2012 at 10:55am
it tell me error,man,i that string to char i cant corvet.
Something else?
Jul 20, 2012 at 10:58am
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?
Jul 20, 2012 at 11:04am
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.
Jul 20, 2012 at 11:08am
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?
Jul 20, 2012 at 11:10am
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/
Jul 20, 2012 at 11:19am
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?>)
Jul 20, 2012 at 11:24am
For example:
s.replace(2,1,"yourString");
Jul 20, 2012 at 11:32am
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="_ _ ! _"

?
Jul 20, 2012 at 11:36am
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 Jul 20, 2012 at 11:38am
Jul 20, 2012 at 11:40am
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.