Alex Crowel
Alex Crowel

Reputation: 97

C programming : Strings

My problem is: Input the string then replace the word that we want to change

For example: input: i love coke

word: coke

replace: pepsi

result: i love pepsi

But when i run this code it crashed. Can you help show me the mistake?

#include <stdio.h>
#include<string.h>
char replace(char s1[100],char s2[100],char s3[100])
{
int k,i,j;
for(i=0;i<strlen(s1);i++)
    for(j=0;j<strlen(s2);j++)
        for(k=0;k<strlen(s3);k++)
    {
        if(s1[i]==s2[j])
        {
          s1[i]=s3[k];
        }
    }
return s3;
}
int main()
{
char s1[100],s2[100],s3[100];
printf("input string:  ");gets(s1);
printf("Find string: ");gets(s2);
printf("Replace: ");gets(s3);
printf("Result: %s",replace(s1,s2,s3));
return 0;
}

Upvotes: 0

Views: 75

Answers (4)

Unavailable
Unavailable

Reputation: 681

First of all, your replace function is returning to char instead of char*. You can also define your function's return type as void and can make it return to char* buffer (in/out) parameter after in-function string operations. Moreover, you can use strtok(), strcmp() and strstr() predefined string.h functions to accomplish any kind of string operations.

Check this out to get information about standart string operation functions: String Operations

Upvotes: 0

Jacob Holloway
Jacob Holloway

Reputation: 887

Your approach doesn't quite work. What you need to do is search the input string for the word you wish to replace. So, before you even start switching things around, you need to search for the whole word you wish to replace.

Once you find that word, you need to then put in your new word in it's place, and then start searching for the word again untill you finish your input string.

So, for pseudo code:

for i in input  //for every letter
    if input[i] != lookfor[0] 
        results[i] put input[i] into new "results" array
    else // We might have found it.
        for j in lookfor  // Go through coke, one at a time
            if input[i+j] != lookfor[j] "c,o,k,e"
                break; //You didn;t find coke, just "c" or "co" or "cok"
        // If you got all the way through, you found coke.
        //So now you have to switch that out for the new that in the result
        results[i] = "pepsi" //Just be careful here, because this has a different index than i, because pespi's length != coke's length

Did that make sense?

Upvotes: 0

Jason Persson
Jason Persson

Reputation: 83

Strings in C are null terminated e.g. "i love coke\0". The string length does not include the null terminator. Because of this you are overwriting the null terminator after the 'e' with the 'i' in "pepsi".

A quick hack to check if null terminating the string would help, is to memset s1, s2, and s3 to 0.

Upvotes: 0

Havenard
Havenard

Reputation: 27854

I suggest you use a 4th buffer to store the generated result. You won't be able to replace locally if the word to be replaced and the new word aren't the same length.

Also, you are comparing characters individually. Just because you found a c doesn't automatically mean you found coke and that you should replace it. You must check the entire word is there before replacing anything. Use strstr() to locate substrings inside a string.

In addition, your function is returning a char, it should return a string (char *).

Furthermore, there are plenty of examples online on how to write a function to replace words on a string, so lets not be reduntant. Google it.

Upvotes: 2

Related Questions