![](/img/trans.png)
[英]How to count unique number of words using fgetc() then printing the count in C
[英]How to count number of unique words in C
我需要计算用户从键盘输入的每个唯一单词的数量。 我应该使用scanf
还是fgets
来存储句子。
我试过使用fgets
并且编译器会抛出错误。
fgets
这是阅读句子的最佳方式,如果您选择此路径,则需要使用strtok
从句子中推断单个单词。
我建议你使用fgets
、 strtok
和strstr
。 我想这是最好的方法。
strtok
和strstr
在strimg.h
header 中定义。
您可以更喜欢scanf
将句子存储在C
程序的变量中。 使用以下代码,您可以存储 50 个字符。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char a[50];
printf("Read a string:\n");
scanf("%[^\n]", a);
printf("\nThe Sentence is; %s", a);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.