替换字符串中的子字符串 - c

时间:2014-12-22 20:36:51

标签: c

我正在尝试执行一个程序,该程序在字符串中查找子字符串并将其替换为用户输入的另一个子字符串。我的代码没有给出编译或运行时错误,但它不起作用。我把printfs放在while循环中,我在它附近写了一条注释行,如果-I在它附近放了另一条注释行,程序就不会进入第一行。它打印a,h和i。循环中的其他部分不起作用。这是我的代码:

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

char *findAndReplace(char *sentence, char *word1, char *word2);

void main()
{
char sentence[1000];
char word1[200];
char word2[200];
int length;

printf("Please enter a sentence: ");
gets(sentence);

printf("Please write the word to be replaced: ");
gets(word1);

printf("Please write the word to be put instead: ");
gets(word2);

findAndReplace(sentence, word1, word2);

system("pause");


}

char* findAndReplace(char *sentence, char *word1, char *word2)
{
char *search, *tempString[1000];
int a, b, c, d, i = 0, j, sentenceLength, word1Length, searchLength;

sentenceLength = strlen(sentence);
printf("Length of %s is %d\n", sentence, sentenceLength);

printf("Finding ");
puts(word1);

search = strstr(sentence, word1);
searchLength = strlen(search);
word1Length = strlen(word1);

strcpy(tempString, sentence);

if(search != NULL)
{
    printf("Starting point: %d\n", sentenceLength - searchLength);
}
else
{
    printf("Eşleşme bulunamadı.\n");
}
j = 0;
while(j < sentenceLength + 1)     //This loop
{
    printf("a");
    if(word1[i] == tempString[j])
    {
        printf("b");

        if(i == word1Length)
        {
            c = j;
            printf("c");

            for(d = 0; d < word1Length; d++)
            {
                tempString[c - word1Length + d + 1] = word2[d];
                printf("d");
            }

            i = 0;
            j++;
            printf("e");
        }

        else
        {      printf("f");
            i++;
            j++;
        }
        printf("g");
    }

    else{
        printf("h");
        i = 0;
        j++;
    }
    printf("i");
}
puts(tempString);
}

2 个答案:

答案 0 :(得分:0)

除此之外,你已经将tempString声明为char* tempString[1000],这是一个未初始化的字符指针数组,所以当你这样做时

strcpy(tempString, sentence); 

你基本上得到了未定义的行为。

在输入字符串时也使用fgets而不是gets - 即使你有相当大的缓冲区,有一天你可能会在文本文件中管道并获得堆栈溢出。

如果我是你,我会用strtok将你的句子分成单词,然后检查每个单词。如果单词相同则替换否则将句子单词添加到新字符串。

e.g。

char newString[1000] = {0};

for (char* word = strtok(sentence, " "); word != NULL; word = strok(NULL, " ")) 
{
  if (!strcmp(word, word1)) // here you may wanna use strncmp or some other variant
  {
    strcat(newString, word2);
  }
  else
  {
     strcat(newString, word);
  }
  strcat(newString, " ");
}

newString[strlen(newString)-1] = '\0';

答案 1 :(得分:0)

你已经取得了一个不错的开局,但是你要做的比你需要的要困难得多。最小化错误的一种方法是在有任何需要完成的工作时依赖标准库函数。例如:

char tempString[1000];
char *search;

search = strstr(sentence, word1);
if (search) {
    ptrdiff_t head_length = search - sentence;
    int sentence_length = strlen(sentence);
    int word1_length = strlen(word1);
    int word2_length = strlen(word2);

    if (sentence_length + word2_length - word1_length < 1000) {
        /* construct the modified string */
        strncpy(tempString, sentence, head_length);
        strcpy(tempString + head_length, word2);
        strcpy(tempString + head_length + word2_length, search + word1_length);
        /* copy it over the original (hope it doesn't overflow!) */
        strcpy(sentence, tempString);
    } else {
        /* oops! insufficient temp space */
    }
} /* else the target word was not found */

仅涵盖搜索/替换位,修复iharob首先指出的tempString类型的错误。此外,它只替换了第一次出现的目标词,因为原始代码似乎试图这样做。