[英]I made a simple program in C which calculates the factorial of a number
[英]c program that calculates the number of different words
我正在尝试制作一个程序,用户将不断给出单独的单词,并且程序将计算所有不同的单词。 例如:用户给出:“Hello”,用户给出:“Bye”,用户给出:“Good”,用户给出:“Bad”,用户给出:“Bye”。 这是4个不同的词。 我希望它在用户完成单词时计算它们。 这是我写的一些代码,但我无法完成它。 我是编程新手,这个问题可能看起来很傻,但对我来说非常重要。
`
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int num=0,b=0,h=0,g=0;
char name[31],text[2000];
while (num==0){
printf("give word: \n");
scanf("%s", name);
printf("%s\n", name);
if (strcmp(name,"*fff*")==0){
num=1;
}
else {
h=h+1;
b=b+strlen(name);
strcpy(text,name);
}
}
return 0;
}
`
与其将单词放在一个巨大的字符数组中,不如创建一个字符数组数组,然后每次检查整个数组是否有重复项。 这将是相对低效的,但它会起作用。
每次阅读一个单词时,将其转换为唯一的 32 位哈希。 在排序的整数数组中搜索比进行 1000 次字符串比较要快得多,也更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.