C 如何调整此算法以处理要修改的关键字的多次出现?

C 如何调整此算法以处理要修改的关键字的多次出现?,c,algorithm,file,file-io,replace,C,Algorithm,File,File Io,Replace,我希望搜索所有出现的字符串(第一个参数),并在所有出现的第一个字符串之前添加另一个字符串(第二个参数) 理想情况下,我希望每次出现的dime都被limedime替换。 然而,我成功地做到了这一点,因为这只是这个词的第一次出现。 任何不是第一个的匹配字符串都不会被检测到,并且不会执行任何操作。另外,包含dime的多行代码会根据对前几行所做的修改进行修改,这不是我想要的 以下是我得到的一些示例输出: something dime something dime something something

我希望搜索所有出现的字符串(第一个参数),并在所有出现的第一个字符串之前添加另一个字符串(第二个参数)

理想情况下,我希望每次出现的
dime
都被
limedime
替换。 然而,我成功地做到了这一点,因为这只是这个词的第一次出现。 任何不是第一个的匹配字符串都不会被检测到,并且不会执行任何操作。另外,包含
dime
的多行代码会根据对前几行所做的修改进行修改,这不是我想要的

以下是我得到的一些示例输出:

something dime something dime something something
将成为

something limedime something dime something something
如果我有这个

dime
notimportant!
dime
dime
我会得到

limedime
notimportant!
limelimedime
limelimelimedime
编辑:我修改了代码,因此您可以使用
stdin
轻松测试它,并且还包括
replace_str()

#包括
#包括
#包括
char*replace_str(char*str,char*orig,char*rep)
{
静态字符缓冲区[4096];
char*p;
如果(!(p=strstrstr(str,orig)))
返回str;
strncpy(缓冲区,str,p-str);
缓冲区[p-str]='\0';
sprintf(buffer+(p-str),%s%s,rep,p+strlen(orig));
返回缓冲区;
}
void replace(char*patternoo,char*replacearoo){
char buff[BUFSIZ];//输入行
char newbuff[BUFSIZ];//任何编辑的结果
字符模式[200];
strcpy(pattern,patternoo);
字符替换[200];
strcpy(替换,替换);
while(fgets(buff、BUFSIZ、stdin)!=NULL){
if(strstrstr(buff,pattern)!=NULL){
//这是我们做模式替换的地方
strcpy(newbuff,replace_str(buff,pattern,strcat(replace,pattern));
}否则{
strcpy(newbuff,buff);
}
printf(“%s”,newbuff);
}
}
int main(){
替换(“一角”、“石灰”);
}

现在,我在想,也许这种方式不太好,因为我只看线条?我不知道我能做什么,一个接一个地读每个字?这对我来说似乎有点过分,但我不太确定。有没有什么快速而肮脏的方法来修复我目前的算法呢?或者我必须重新开始并采取一种全新的方法吗?

如果在每次出现
dime
之前插入
lime
,则需要读取一行,在输入缓冲区中找到每次出现的
dime
,找到后,将输入缓冲区中未处理的部分复制到输出缓冲区,然后添加
lime
,然后添加
dime
,然后在
dime
之后继续搜索

这意味着:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static void replace(char *pattern, char *replace)
{
    char buff[BUFSIZ];      // the input line
    char newbuff[BUFSIZ];   // the results of any editing
    size_t replen = strlen(replace);
    size_t patlen = strlen(pattern);

    while (fgets(buff, BUFSIZ, stdin) != NULL)
    {
        newbuff[0] = '\0';
        char *dst = newbuff;
        char *data = buff;
        char *patt;
        while ((patt = strstr(data, pattern)) != NULL)
        {
            memmove(dst, data, (patt - data));
            dst += (patt - data);
            memmove(dst, replace, replen);
            dst += replen;
            memmove(dst, pattern, patlen);
            dst += patlen;
            data = patt + patlen;
        }
        *dst = '\0';
        printf("%s%s", newbuff, data);
    }
}

int main(void)
{
    replace("dime", "lime");
    return 0;
}
运行程序(
repstr
,我称之为)的输出是:

$。/repstr
假设在每次出现
dime
之前插入
lime
,则需要读取一行,在输入缓冲区中找到每次出现的
dime
,找到后,将输入缓冲区的未处理部分复制到输出缓冲区,然后添加
lime
,然后添加
dime
,然后在
dime
后继续搜索

这意味着:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static void replace(char *pattern, char *replace)
{
    char buff[BUFSIZ];      // the input line
    char newbuff[BUFSIZ];   // the results of any editing
    size_t replen = strlen(replace);
    size_t patlen = strlen(pattern);

    while (fgets(buff, BUFSIZ, stdin) != NULL)
    {
        newbuff[0] = '\0';
        char *dst = newbuff;
        char *data = buff;
        char *patt;
        while ((patt = strstr(data, pattern)) != NULL)
        {
            memmove(dst, data, (patt - data));
            dst += (patt - data);
            memmove(dst, replace, replen);
            dst += replen;
            memmove(dst, pattern, patlen);
            dst += patlen;
            data = patt + patlen;
        }
        *dst = '\0';
        printf("%s%s", newbuff, data);
    }
}

int main(void)
{
    replace("dime", "lime");
    return 0;
}
运行程序(
repstr
,我称之为)的输出是:

$。/repstr
一种方法可能是:

Have a temp string that displays output.

Until whole sentence read
   Read complete word of that sentence.
   if that word == dime
      append limedime to temp string
   else append the same word to temp string.
试运行:

input: something dime somthing lime dime

iteration1: something read compare it with lime, they arent equal so append somthing to temp string.
temp: something

iteration2: word read: dime
temp: something limedime

iteration3: word read: something
temp: something limedime something

and soo on.
希望这种方法有帮助:)
很久没有接触过C了,所以我忘记了它的语法,所以无法帮助编写pseudo应该就足够了。

一种方法可能是:

Have a temp string that displays output.

Until whole sentence read
   Read complete word of that sentence.
   if that word == dime
      append limedime to temp string
   else append the same word to temp string.
试运行:

input: something dime somthing lime dime

iteration1: something read compare it with lime, they arent equal so append somthing to temp string.
temp: something

iteration2: word read: dime
temp: something limedime

iteration3: word read: something
temp: something limedime something

and soo on.
希望这种方法有帮助:)

很久没有接触过C了,所以我忘记了它的语法,所以不能帮助编码pseudo应该就足够了。

(a)你需要一个循环,而不是
if
围绕你的
str()
调用-这就是为什么你每行只能得到一个替换。(b) 由于字符串在增长,因此必须小心处理目标(不能就地完成)。您的代码比MCVE()更复杂,因为它正在打开文件-对于MCVE,最好(更简单,因此更简单)从标准输入读取,然后只写入标准输出。文件处理是本练习的一个单独部分,一旦基本工作就绪,就可以轻松修复。编写更多函数可能是值得的。“limelimelimedime”问题是由于
strcat(replace,pattern)
while
循环中。解决方案是不可能知道的,因为您描述的所有问题都来自对
replace\u str
的调用,并且您还没有显示
replace\u str
的代码。也就是说,我同意JonathanLeffler的观点,即您需要发布一个@user3386109是的,我已经编辑了问题和代码。@JonathanLeffler编辑了问题。另外,我不太确定如何在这里实现循环。下面的else语句很重要,我在实现while循环时的拙劣尝试让我犯了一些错误。我很佩服你对编写strcpy(newbuff,replace_str(buff,pattern,strcat(replace,pattern))这行代码的信心-我当然不知道那是在做什么;我甚至不知道它应该做什么。就这样说吧,;我想你在做什么