C 用另一个给定单词替换文本中的一个单词

C 用另一个给定单词替换文本中的一个单词,c,C,我这里有这个密码 #include <stdio.h> #include <string.h> #include<conio.h> void main() { char s[] = "All work and no play makes Jack a dull boy."; char word[10],rpwrd[10],str[10][10]; int i=0,j=0,k=0,w,p; printf("All work and no pl

我这里有这个密码

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

void main()
{
  char s[] = "All work and no play makes Jack a dull boy.";
  char word[10],rpwrd[10],str[10][10];
  int i=0,j=0,k=0,w,p;

  printf("All work and no play makes Jack a dull boy.\n");
  printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
  scanf("%s",word);
  printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
  scanf("%s",rpwrd);
  p=strlen(s);

  for (k=0; k<p; k++)
    {
      if (s[k]!=' ')
        {
          str[i][j] = s[k];
          j++;
        }
      else
        {
          str[i][j]='\0';
          j=0; i++;
        }
    }

  str[i][j]='\0';
  w=i;

  for (i=0; i<=w; i++)
    {
      if(strcmp(str[i],word)==0)
        strcpy(str[i],rpwrd);

      printf("%s ",str[i]);
    }
  getch();
}
没有搜索整个句子


thx

最快的方法是分配一个新字符串,即
strlen(s)-strlen(word)+strlen(rpwrd)+1
。然后使用
strstr
函数查找要替换的单词,并将其复制到新字符串中,添加新单词,然后将原始句子的其余部分复制到新字符串中。

您需要声明一个
char*
并使用
malloc
calloc
为其分配动态内存。当单词比被替换的单词长时,会出现此错误。
其次,
中有许多函数用于搜索字符串并将其替换为给定字符串。 有一个
strstrstr
-函数,用于定位子字符串并返回字符串中的位置。 虽然它是关于
C++
,但请在
-标题上查找参考

没有搜索整个句子

您必须搜索整行:

char sentence[] = "The quick brown fox jumped over the lazy dog.";
const char *to_replace = "fox";
const char *replacement = "dragon";

char *pos = strstr(sentence, to_replace);

// if found
if (pos != NULL) {
    // The new string
    size_t newlen = strlen(sentence) - strlen(to_replace) + strlen(replacement);
    char new_sentence[newlen + 1];

    // Copy the part of the old sentence *before* the replacement
    memcpy(new_sentence, sentence, pos - sentence);

    // Copy the replacement
    memcpy(new_sentence + (pos - sentence), replacement, strlen(replacement));

    // Copy the rest
    strcpy(new_sentence + (pos - sentence) + strlen(replacement), pos + strlen(to_replace));

    printf("Old: %s\nNew: %s\n", sentence, new_sentence);
}
#包括
#包括
int main()
{
char str[]=“这是一个用简单代码生成的简单字符串”;
char*pch;
int i=0,count=0;
对于(i=0;i这可能有效:

 #include<bits/stdc++.h>
 using namespace std;
 int main()
 {
    char str[] ="xxxxforxxxx xxxx for xxxx";
    int n=sizeof(str)/sizeof(str[0]);
    string old ="xxxx";
    int oldlen=old.length();
    string news ="geeks";
    string final="";
    for(int i=0;i<=n-oldlen-1;i++)
    {
        string check="";
        for(int j=i;j<old.length()+i;j++)
        {
            check+=str[j];
        }
        if(old==check)
        {
           final+=news;
           i=i+oldlen-1;
        }
        else
        {
           final+=str[i];
        }
    }
    cout<<final;
 }
#包括
使用名称空间std;
int main()
{
字符str[]=“xxxxforxxxx xxxx xxxx for xxxx”;
int n=sizeof(str)/sizeof(str[0]);
字符串old=“xxxx”;
int oldlen=old.length();
string news=“极客”;
字符串final=“”;

对于(inti=0;i
chars[]=“只工作,不玩耍,聪明的孩子也变傻。”;
-这里,
s
不是只读的,写入它就可以了。而且,自从C99以来,没有必要
malloc()
对于这样一个简单的用例。从技术上讲,你不必搜索整行,只需向上搜索,直到找到要替换的单词。@iain,如果是结尾,那就是整行。应该对最坏的情况做出假设,并且字符串处理算法(如本算法)通常在
O(n)中运行
。您应该添加一些解释/注释。
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string made with simple code";
  char * pch;
  int i=0,count=0;
  for(i=0;i<strlen(str);i++){
    if(str[i]=='s'&& str[i+1]=='i'&&str[i+2]=='m'&&str[i+3]=='p' && str[i+4]=='l' && str[i+5]=='e'){
        count++;
      }
  }
  for(i=1;i<=count;i++){
    pch = strstr (str,"simple");
    strncpy (pch,"sample",6);
  }

  puts (str);
  return 0;
}
 #include<bits/stdc++.h>
 using namespace std;
 int main()
 {
    char str[] ="xxxxforxxxx xxxx for xxxx";
    int n=sizeof(str)/sizeof(str[0]);
    string old ="xxxx";
    int oldlen=old.length();
    string news ="geeks";
    string final="";
    for(int i=0;i<=n-oldlen-1;i++)
    {
        string check="";
        for(int j=i;j<old.length()+i;j++)
        {
            check+=str[j];
        }
        if(old==check)
        {
           final+=news;
           i=i+oldlen-1;
        }
        else
        {
           final+=str[i];
        }
    }
    cout<<final;
 }