字符串c中的单词替换
Word replace in a string c
如何更改此代码以替换字符串中出现的每个单词但不会替换所有子字符串。例如,如果单词 ask 不会替换 task 或 asking。因此,对于输入:"I'm asking ask a task ask my friend" 和替换词:"for",输出应为:"I'm asking for a task for my friend".
char *replace_word(char *string, char *word, char *new_word) {
int len = strlen(string) + 1;
char *temp = malloc(len * sizeof(char));
int temp_len = 0;
char *found;
int len_w = strlen(word);
while (found = strstr(string, word)) {
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
else {
memcpy(temp + temp_len, string, found - string);
temp_len = temp_len + found - string;
string = found + strlen(word);
len = len - strlen(word) + strlen(new_word);
temp = realloc(temp, len * sizeof(char));
memcpy(temp + temp_len, new_word, strlen(new_word));
temp_len = temp_len + strlen(new_word);
}
}
strcpy(temp + temp_len, string);
return temp;
}
在这个阶段,如果输入是:"It's ask me this task?"就可以了。输出是:“这是给我的这个任务?”但如果输入是这样的:"I'm asking this ask a friend",输出将与输入相同,因此代码不会进行更改。需要帮忙!
好的,所以发生的事情是,在它发现子字符串中出现该单词后,while 循环中断;
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
相反,它应该复制单词和它之前的所有内容,这样 while 就不会进入无限循环,因为它会一遍又一遍地找到相同的事件。
此更改应该可以正常工作:
char *replace_word(char *string, char *word, char *new_word) {
int len = strlen(string) + 1;
char *temp = malloc(len * sizeof(char));
int temp_len = 0;
char *found;
int len_w = strlen(word);
while (found = strstr(string, word)) {
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
memcpy(temp + temp_len, string, found - string + strlen(word));
temp_len = temp_len + found - string + strlen(word);
string = found + strlen(word);
}
else {
memcpy(temp + temp_len, string, found - string);
temp_len = temp_len + found - string;
string = found + strlen(word);
len = len - strlen(word) + strlen(new_word);
temp = realloc(temp, len * sizeof(char));
memcpy(temp + temp_len, new_word, strlen(new_word));
temp_len = temp_len + strlen(new_word);
}
}
strcpy(temp + temp_len, string);
return temp;
}
您的代码还有一些其他问题,但是:
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
这是核心问题 - 一旦您发现这个词被字母数字字符包围,您就想跳到下一个出现的地方,但您在这里所做的是完全打破循环。
您想做的是:
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
continue;
}
'break' 中断 循环。
'continue' 继续 到下一次迭代。
确保在找到每个匹配项后更新字符串,否则您将陷入从 strstr() 函数一遍又一遍地获取相同指针的无限循环中。
如何更改此代码以替换字符串中出现的每个单词但不会替换所有子字符串。例如,如果单词 ask 不会替换 task 或 asking。因此,对于输入:"I'm asking ask a task ask my friend" 和替换词:"for",输出应为:"I'm asking for a task for my friend".
char *replace_word(char *string, char *word, char *new_word) {
int len = strlen(string) + 1;
char *temp = malloc(len * sizeof(char));
int temp_len = 0;
char *found;
int len_w = strlen(word);
while (found = strstr(string, word)) {
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
else {
memcpy(temp + temp_len, string, found - string);
temp_len = temp_len + found - string;
string = found + strlen(word);
len = len - strlen(word) + strlen(new_word);
temp = realloc(temp, len * sizeof(char));
memcpy(temp + temp_len, new_word, strlen(new_word));
temp_len = temp_len + strlen(new_word);
}
}
strcpy(temp + temp_len, string);
return temp;
}
在这个阶段,如果输入是:"It's ask me this task?"就可以了。输出是:“这是给我的这个任务?”但如果输入是这样的:"I'm asking this ask a friend",输出将与输入相同,因此代码不会进行更改。需要帮忙!
好的,所以发生的事情是,在它发现子字符串中出现该单词后,while 循环中断;
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
相反,它应该复制单词和它之前的所有内容,这样 while 就不会进入无限循环,因为它会一遍又一遍地找到相同的事件。 此更改应该可以正常工作:
char *replace_word(char *string, char *word, char *new_word) {
int len = strlen(string) + 1;
char *temp = malloc(len * sizeof(char));
int temp_len = 0;
char *found;
int len_w = strlen(word);
while (found = strstr(string, word)) {
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
memcpy(temp + temp_len, string, found - string + strlen(word));
temp_len = temp_len + found - string + strlen(word);
string = found + strlen(word);
}
else {
memcpy(temp + temp_len, string, found - string);
temp_len = temp_len + found - string;
string = found + strlen(word);
len = len - strlen(word) + strlen(new_word);
temp = realloc(temp, len * sizeof(char));
memcpy(temp + temp_len, new_word, strlen(new_word));
temp_len = temp_len + strlen(new_word);
}
}
strcpy(temp + temp_len, string);
return temp;
}
您的代码还有一些其他问题,但是:
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
break;
}
这是核心问题 - 一旦您发现这个词被字母数字字符包围,您就想跳到下一个出现的地方,但您在这里所做的是完全打破循环。
您想做的是:
if ((isalnum(*(found - 1))) || (isalnum(*(found + len_w)))) {
continue;
}
'break' 中断 循环。
'continue' 继续 到下一次迭代。
确保在找到每个匹配项后更新字符串,否则您将陷入从 strstr() 函数一遍又一遍地获取相同指针的无限循环中。