我试图计算一个字符在字符串中重复的次数(可以是多个单词),但是,现在我认为我非常接近结果,但我的输出是有点奇怪:
CODE:
int statistics(){
char str[122];
int i=0;
int count[122] = { 0 };
printf("Enter a string: ");
fgets(str,sizeof str, stdin);
if(fgets(str,sizeof str, stdin) == NULL){
printf("error");
}
size_t size = strlen(str);
for(int i = 0; i < size; i++) {
count[(int)str[i]] += 1;
}
for (i = 0; i < 122; i++) {
if(count[i]>=1){
printf("The %d. character has %d occurrences.\n", i, count[i]);
}
}
strtok(str,"\n");
printf("'%s' is %lu characters long\n",str,size-1);
}
例如输入:你好
输出:
The 107. character has 1 occurences.
The 198. character has 1 occurences.
The 201. character has 1 occurences.
The 205. character has 1 occurences.
The 208. character has 1 occurences.
'Hello' is 5 characters long
答案 0 :(得分:1)
您正在使用count
中索引的当前编码。如果您的编码为ASCII(最常见的编码),则字符'H'
的值为72
,因此您将增加count[72]
,并且当您打印输出时,您将打印它是第72个角色。
而是使用循环变量作为计数器,然后使用str[i]
中的字符作为计数器数组的索引。
像这样的东西
for (size_t i = 0; i < strlen(str); ++i)
{
if (isprint(str[i])
{
printf("Character #%zu is '%c' and the count is %d\n",
i + 1, str[i], count[str[i]]);
}
else
{
printf("Character #%zu is 0x%02hhx and the count is %d\n",
i + 1, str[i], count[str[i]]);
}
}
顺便说一句,要小心fgets
的输入,它很可能会将换行符留在字符串的末尾,你也会计算它。我使用isprint
处理上面的代码,看看该字符是否“可打印”。
此外,更重要的是,当您在count
数组中使用字符编码作为索引时,您需要与编码中的字符一样多的元素。使用ASCII encoding(最常见且可能正在使用的),您需要128个元素来计算ASCII table中所有可能的字符(不包括“扩展ASCII”)。因此,您需要声明具有128个元素的count
数组。如果不这样做,那么您可能会将count
数组的索引编入索引,并且未定义的行为。
答案 1 :(得分:0)
您的输出“奇怪”的原因是您使用%d
打印字符代码,而不是%c
来打印字符本身:
printf("The '%c' (code:%d) character has %d occurrences.\n", i, i, count[i]);
The 'e' (code:101) character has 1 occurrences.
The 'h' (code:104) character has 1 occurrences.
The 'l' (code:108) character has 2 occurrences.
The 'o' (code:111) character has 1 occurrences.
'hello' is 5 characters long