一个示范程序大致如下:
#include
#include
#include
int main(int argc, char **argv)
{
char s1[] = "This is test sentence, find all duplicated words.";
char s2[] = "Just a test for finding duplication words.";
size_t len_s1 =0;
char *s_dup = NULL;
char* pch = NULL;
char* psubstr = NULL;
/* make a duplication to protect the source */
len_s1 = strlen(s1);
s_dup = (char*)malloc(len_s1 * sizeof(char));
if (s_dup == NULL)
{
fputs("Memory allocating error", stderr);
}
strncpy(s_dup, s1,len_s1);
/* slice the dup. string, loop thru 2nd string to compare word by word */
pch = strtok (s_dup, " ,."); /* modifying if necessary */
while (pch != NULL)
{
/* printf("%s\n", pch); */ /* debug print */
psubstr = strstr(s2, pch);
if (psubstr != NULL)
printf ("--->%s\n",pch);
pch = strtok (NULL, " ,.");
}
/* release memory allocated by malloc above */
free(s_dup);
return 0;
}
追问:
如果不用指针该怎么做
追答:
① 哪一部分不希望是指针? ② 为什么不希望是指针? C的特点之一啊?
作业帮用户
2016-12-11
举报