示例代码如下:
#include
#include
int main()
{
char sentence[100] = "This is a example sentence.";
char old_word[20] = "example";
char new_word[20] = "sample";
char *word;
// 将空格作为分割符,获取每个单词
word = strtok(sentence, " ");
while(word != NULL)
{
// 判断当前单词是否需要替换
if(strcmp(word, old_word) == 0)
{
strcpy(word, new_word);
}
// 继续获取下一个单词
word = strtok(NULL, " ");
}
// 输出已经替换的句子
printf("%s\n", sentence);
return 0;
}