2

我这里有这段代码

#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();
}

我怎样才能替换“杰克”这个词?喜欢

输出:

All work and no play makes Jack a dull boy.

Enter ther word Jack to be Replaced
Mark
Tom

All work and no play makes Mark a dull boy.
All work and no play makes Tom a dull boy.

无需搜索整个句子。

谢谢

4

5 回答 5

3

无需搜索整个句子。

您必须搜索整行:

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);
}
于 2013-03-17T20:28:31.123 回答
0

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

于 2013-03-17T20:27:14.010 回答
0

您需要声明 a并使用orchar*为其分配动态内存。当单词比替换的单词长时,就会出现这种情况。 其次,有很多函数可以搜索字符串并将其替换为给定的字符串。有一个-function 定位子字符串并返回字符串内的位置。虽然它是关于,但请在此处查找有关-Header 的参考。malloccalloc
<string.h>strstrC++<string.>

于 2013-03-17T20:28:12.280 回答
0
#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;
}
于 2017-11-19T05:09:46.620 回答
-1

这可能有效:

 #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];
        }
    }
    final += str[str.Length-1];
    cout<<final;
 }
于 2020-01-11T16:00:08.893 回答