【发布时间】:2020-05-14 06:00:59
【问题描述】:
程序应以相同的顺序包含表中的单词 它们出现在文本中。 使用string.h、ctype.h、stdio.h,包含strtok函数
#include<ctype.h>
int main(void)
{
int i,j;
char text[3][80];
char wordList[120][80];
int count = 0;
char* ptr;
for (i = 0; i <= 2; i++) {
gets(&text[i][0]);
}
for (i = 0; i <= 2; i++) {
for (j = 0; text[i][j]!='\0' ; j++) {
text[i][j] = tolower(text[i][j]);
}
}
ptr = strtok(text, " ,.;:!?-()[]<>");
while (ptr != NULL) {
}
想了很久,不知道怎么试。你可以问我的代码有什么问题,但我根本不知道方法......
【问题讨论】:
-
在更进一步之前,请先查看Why gets() is so dangerous it should never be used!,并认真思考是否
gets(您将替换为fgets())——不能简单地使用text[i]而不是&text[i][0]? -
方法:1)使用
strtok获取下一个单词。 2) 检查单词是否已经在wordList中。 3) 如果找到,则增加该单词的计数。 4) 如果没有找到,将单词添加到wordList并将计数器设置为 1。只要输入中有单词就重复此操作。 5) 打印wordList和相应的计数。
标签: c arrays string pointers word