简体   繁体   English

用字符串中的一个词替换另一个词

[英]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.我希望 output 如下所示。 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.我的意思是我只想替换那个特定的词,就像它在字符串中出现的次数一样多,例如这里的 good 一词。 Can anyone help me identify the mistake or help me find another way.谁能帮我找出错误或帮我找到另一种方法。

You should not use gets , it's dangerous: Why is the gets function so dangerous that it should not be used?你不应该使用gets ,它很危险: 为什么 gets function 如此危险以至于不应该使用它? . .

You should use fgets to read from stdin instead.您应该改用fgetsstdin读取。

Int the word function, you compare length of two string using strlen funciton:word function 中,您使用strlen函数比较两个字符串的长度:

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

But in the main function, you call it as:但是在主要的 function 中,你称它为:

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. b[i]是字符数组,但你不要在每个单词的末尾添加 null 字符\0 So strlen will not work as you think.所以strlen不会像你想的那样起作用。

In the code that you replace string c by d .在将字符串c替换为d的代码中。 You not update value of e array.您不更新e数组的值。 I think, e[i] should be:我认为, e[i]应该是:

e[i] = strlen(d);

I seem that you do not use some standard function for string, for example, very useful for your program.我似乎没有使用一些标准的 function 作为字符串,例如,对您的程序非常有用。

strcmp to compare string and string. strcmp比较字符串和字符串。

strstr to find the word in the string. strstr在字符串中查找单词。

If you search in google, you can have a ton of code for your case.如果你在 google 中搜索,你可以为你的案例找到大量代码。

for example: program to Replace a word in a text by another given word例如: 程序用另一个给定的词替换文本中的一个词

another: Replacing Words inside string in C另一个: 替换字符串中的单词 C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM