October 30th, 2012, 03:03 PM
Help please *Strings*
Hello there, hope you could help me out with my question about strings.
So my question is..
I have 3 strings , I need to find the second string in the first one(search one string in another string) and then to replace it with the third string.
a = abcde
b = cd
c = xx
The result need to be: a = abxxe
Tnx for help :) :hi:
October 30th, 2012, 03:12 PM
Assuming length of b and c are the same, use strstr() and strncpy().
If the length are different, you need to "complicate" the strncpy(), by using a user-defined function (or code) instead :)
Last edited by bdb; October 30th, 2012 at 03:20 PM.
October 30th, 2012, 03:16 PM
Consider that the string will always be split into 3 logical components
Both <pre> and/or <post> can be empty strings.
Now, if you do
x = strstr(a,b);
Then using pointer arithmetic
x - a
gives you the length of <pre>
x + strlen(b)
gives you the start of <post>
Once you know these things, the rest is just carefully chosen strcpy or strncpy calls to assemble the new string.