c程序删除字符串中重复的单词
remove duplicated words in string in c program
我想写一个函数来获取句子(我称之为重句)并找到重复的单词,并将它们删除。
前任:
输入
Big happy happy smile
输出
Big happy smile
这表明行中有错误:
if (strstr(temp1, temp) == NULL)
希望有人能找到是什么东西。
void RemoveDuplicates(char *resentence)
{
char *temp1 = malloc(100);
char *temp = NULL;
int len;
len = strlen(resentence);
printf("len:%d", len);
temp = strtok(resentence, " ");
if (temp != NULL && strstr(temp1, temp) == NULL)
strcpy(temp1, temp);
while (temp != NULL)
{
temp = strtok(NULL, " ");
if (strstr(temp1, temp) == NULL)
{
strcat(temp1, " ");
strcat(temp1, temp);
}
}
strcpy(resentence,temp1);
puts(resentence);
free(temp1);
}
试试这个
void RemoveDuplicates(char *resentence){
char *temp1 = malloc(100);
char *temp = NULL;
*temp1=0;
temp = strtok(resentence, " ");
if (temp != NULL){// && strstr(temp1, temp) == NULL)
strcpy(temp1, temp);
while ((temp = strtok(NULL, " ")) != NULL){
if (strstr(temp1, temp) == NULL){
strcat(temp1, " ");
strcat(temp1, temp);
}
}
}
strcpy(resentence, temp1);
puts(resentence);
free(temp1);
}
实际上,如果您在这段代码中输入这样的内容:"abc ab",即使在更正后的版本中,它仍然会给出错误的输出,在这种情况下:"abc"(missing "ab").发生这种情况是因为函数 "strstr()" 的工作原理。它需要两个字符串,假设 char *a = "abc" 和 char *b = "ab",然后逐个字符地将它们相互比较。对于字符串 a 和 b,它会是:1) a = a (ok), 2) b = b (ok) 在这里它停止一次 "strstr()" 不比较空终止符 '\0' 让我们错误地确认了平等。以下是我为同样的目的编写的一个小程序,似乎可以处理任何给定的输入(至少我试过的输入:]),希望它有所帮助。
#include <stdio.h>
#include <string.h>
#define SIZE 500
void duplicateRemover(char *, const int);
int main(void)
{
char someString[SIZE];
puts("Enter text: ");
fgets(someString, SIZE, stdin);
someString[strcspn(someString, "\n")] = 0;
printf("\n\n%s\n\n", "Text without repeated words: ");
duplicateRemover(someString, SIZE);
}
void duplicateRemover(char *arrayPtr, const int sizeP)
{
char wordTable[sizeP][sizeP], *tokPtr;
size_t i, j, k, l;
tokPtr = strtok(arrayPtr, " ");
strcpy(wordTable[0], tokPtr);
for(i = 1; (tokPtr = strtok(NULL, " ")) != NULL; i++)
strcpy(wordTable[i], tokPtr);
for(j = 0; j <= i; j++)
for(k = j + 1; k <= i; k++)
if(strcmp(wordTable[j], wordTable[k]) == 0) {
for(l = k; l < i; l++)
strcpy(wordTable[l], wordTable[l + 1]);
k = j;
i--;
}
for(l = 0; l <= i; l++)
printf("%s ", wordTable[l]);
}
我想写一个函数来获取句子(我称之为重句)并找到重复的单词,并将它们删除。 前任: 输入
Big happy happy smile
输出
Big happy smile
这表明行中有错误:
if (strstr(temp1, temp) == NULL)
希望有人能找到是什么东西。
void RemoveDuplicates(char *resentence)
{
char *temp1 = malloc(100);
char *temp = NULL;
int len;
len = strlen(resentence);
printf("len:%d", len);
temp = strtok(resentence, " ");
if (temp != NULL && strstr(temp1, temp) == NULL)
strcpy(temp1, temp);
while (temp != NULL)
{
temp = strtok(NULL, " ");
if (strstr(temp1, temp) == NULL)
{
strcat(temp1, " ");
strcat(temp1, temp);
}
}
strcpy(resentence,temp1);
puts(resentence);
free(temp1);
}
试试这个
void RemoveDuplicates(char *resentence){
char *temp1 = malloc(100);
char *temp = NULL;
*temp1=0;
temp = strtok(resentence, " ");
if (temp != NULL){// && strstr(temp1, temp) == NULL)
strcpy(temp1, temp);
while ((temp = strtok(NULL, " ")) != NULL){
if (strstr(temp1, temp) == NULL){
strcat(temp1, " ");
strcat(temp1, temp);
}
}
}
strcpy(resentence, temp1);
puts(resentence);
free(temp1);
}
实际上,如果您在这段代码中输入这样的内容:"abc ab",即使在更正后的版本中,它仍然会给出错误的输出,在这种情况下:"abc"(missing "ab").发生这种情况是因为函数 "strstr()" 的工作原理。它需要两个字符串,假设 char *a = "abc" 和 char *b = "ab",然后逐个字符地将它们相互比较。对于字符串 a 和 b,它会是:1) a = a (ok), 2) b = b (ok) 在这里它停止一次 "strstr()" 不比较空终止符 '\0' 让我们错误地确认了平等。以下是我为同样的目的编写的一个小程序,似乎可以处理任何给定的输入(至少我试过的输入:]),希望它有所帮助。
#include <stdio.h>
#include <string.h>
#define SIZE 500
void duplicateRemover(char *, const int);
int main(void)
{
char someString[SIZE];
puts("Enter text: ");
fgets(someString, SIZE, stdin);
someString[strcspn(someString, "\n")] = 0;
printf("\n\n%s\n\n", "Text without repeated words: ");
duplicateRemover(someString, SIZE);
}
void duplicateRemover(char *arrayPtr, const int sizeP)
{
char wordTable[sizeP][sizeP], *tokPtr;
size_t i, j, k, l;
tokPtr = strtok(arrayPtr, " ");
strcpy(wordTable[0], tokPtr);
for(i = 1; (tokPtr = strtok(NULL, " ")) != NULL; i++)
strcpy(wordTable[i], tokPtr);
for(j = 0; j <= i; j++)
for(k = j + 1; k <= i; k++)
if(strcmp(wordTable[j], wordTable[k]) == 0) {
for(l = k; l < i; l++)
strcpy(wordTable[l], wordTable[l + 1]);
k = j;
i--;
}
for(l = 0; l <= i; l++)
printf("%s ", wordTable[l]);
}