ananya
ananya

Reputation: 25

replace one word with another in a string

can anyone say me what is the mistake here. I used two dimensional array here.

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char a[100], b[30][30], c[20], d[20];
        int i, j=0, k, count=0;
        int e[30];
        printf("enter a string: ");
        gets(a);
        printf("enter the word which has to be replaced: ");
        gets(c);
        printf("enter the word with which it has to be replaced: ");
        gets(d);
        for(i=0,k=0, e[i]=0; i<strlen(a); i++, k++)
        {
            if(a[i]==' ')
            {
                k=0;
                j++;
                count++;
            }
            else
            {
                b[j][k]=a[i];
                e[i]++;
            }
        }

        for(i=0; i<j; i++)
        {
            if(word(b[i], c)==0)
            {
                for(k=0; k<strlen(d); k++)
                {
                    b[i][k]=d[k];
                }
            }
        }
        for(i=0; i<count; i++)
        {
            for(j=0; j<e[i]; j++)
            {
                printf("%c", b[i][j]);
            }
            printf(" ");
        }
    }
    int word(char *a, char *c)
    {
        int i;
        if(strlen(a)==strlen(c))
        {
            for(i=0; i<strlen(a); i++)
            {
                if(a[i]!=c[i])
                {
                    return 1;
                }
            }
        }
        else
            return 2;
        return 0;
    }

I want the output to be like the below. For example:

    enter a string: vicky is a good boy
    enter the word which has to be replaced: good
    enter the word with which it has to be replaced: bad
    vicky is a bad boy

I mean I want to replace only that particular word, as many times as it appears in the string such as here, the word good. Can anyone help me identify the mistake or help me find another way.

Upvotes: 0

Views: 396

Answers (1)

Hitokiri
Hitokiri

Reputation: 3699

You should not use gets, it's dangerous: Why is the gets function so dangerous that it should not be used?.

You should use fgets to read from stdin instead.

Int the word function, you compare length of two string using strlen funciton:

if(strlen(a)==strlen(c))

But in the main function, you call it as:

if(word(b[i], c)==0)

b[i] is array of character, but you do not add null character \0 at the end of each word. So strlen will not work as you think.

In the code that you replace string c by d. You not update value of e array. I think, e[i] should be:

e[i] = strlen(d);

I seem that you do not use some standard function for string, for example, very useful for your program.

strcmp to compare string and string.

strstr to find the word in the string.

If you search in google, you can have a ton of code for your case.

for example: program to Replace a word in a text by another given word

another: Replacing Words inside string in C

Upvotes: 1

Related Questions