Hello guys, is there any function to search for a word within a string and replace it with another? (Of different sizes)

example:
1 string [] = "bla ble ble ble bli blo"
word to replace = bli by 123456
2 string [] = "bla ble ble ble 123456 blo"

No there isn't. There is one to search for a string within a larger string, however.

The general logic would be:

#include <string.h>

use strstr() to locate the word you want to replace. Save the position (pointer) it returns when it finds it. From the start of the string, to the one returned, is the leftSide of the string. Everything to the right of the returned pointer, is the rightSide of the string.

You want to work with these strings in a char array with extra length, because you'll need to shift some letters left or right, to make everything fit correctly, if the string being replaced doesn't happen to be the same length as the string being inserted.

Use strlen() to measure the length of the string you want replaced, and the string you want to insert. Subtract them from each other. If the string being added is longer, shift the letters on the rightSide of the string, to the right, that many spaces. If it's shorter, shift them to the left that number of spaces.

Now with the spacing correct, overwrite the left most spaces in the rightSide part of the string, with the string you want to add.

If you've done it correctly, the string will now be correct. Be sure when you are shifting letters, to also shift the end of string char - even though you can't see it.

Edited 3 Years Ago by Dani: Formatting fixed

I do it without shifting by using a temporary character array. After calling strstr() to find the beginning of the word or string you want to replace, copy the original string from the beginning to the position returned by strstr().

For example, If the original string is "Now is the time for ..." and you want to replace the word "time" you would copy "Now is the " into another character array.

Next, add the word or string you want to replace it with, if any. Lets say you want to replace "time" with "moment". You would concantinate "moment " to the end of the temporary character array.

Next step is to add all the remainder of the original string to the temp character array. In this example "for ...".

At this point the temp array contains "Now is the moment for ...". Now you can do anything you want with this temp array, such as copy it back to the original buffer (note: you can not copy back over a string literal.)

I tried the way the ancient, was well, but did not work.

int i;        
                char * p;
                p = strstr(string,name);  

                for(i=0; string[i] != * pon1 ; i++)
                {
                       stringnew[i] = string[i];
                }    

                strcat(stringnew,newname);

Could understand, sorry I'm starting to program ^ ^ Thanks for the answers, I just need to understand how to get the last part, how to put the pointer at the end of the word to copy what next.

On line #9, add

stringnew[i]='\0';

so the end of string char is in place. Now recompile, and see what you get.

If you are adding newname ONLY to the right hand side, then it should work as is. If you are adding newname into the middle portion of a string of text, then you will still need to add another strcat(), to append the right side of the original text, onto the stringnew.

If that fails, be specific what it's showing for a final result.

I arrived in this algorithm. The problem is to get the final part.

                int i;        
                char * pon1;
                pon1 = strstr(string,name);  
                for(i=0; string[i] != * pon1 ; i++)
                {
                       stringnew[i] = string[i];

                }
                stringnew[i]='\0';  
                cop(stringnew, stringnew2);  // function for coppy stringnew in stringnew2
                strcat(stringnew,name2);     //name2 = greater name for replace

                for(i = (sizeof(stringnew2))+ (sizeof(name)); string[i] != '\n'; i++)
                {
                  stringnew4[i] = string[i];   //stringnew4 - last part       
                }
                stringnew4[i]='\0'; 
                strcat(stringnew, stringnew4);
                printf("%s", stringnew);

line 4: the loop won't work because it's checking if the character string[i] is the same as the first character in pon1. Let's assume the character at *pon1 is the letter 'a'. How many instances of 'a' do you think might be in the string??? There could be lots of them, so testing for string[i] == 'a' will likely be a false positive hit.

What I would do is just truncaqte string at pon1 then you can just simply strcpy() string into stringnew

*pon1++ = '\0';
strcpy(stringnew,string);

At this point you need to move pon1 pointer to the end of the characters that are in name. Example, if name == "time" then increment pon1 until it is one character beyond the end of the word "time". Now you will have the start of the last part to be copied into stringnew array. You can call strcat() to copy all the characters from pon1 into stringnew.

Edited 3 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.