[英]How to count the number of different type of characters in file using C.
The characters may contain any numeric, alphabets, symbols such as :;@ etc. one method is to use a switch case statement as show below. 字符可以包含任何数字,字母,符号,例如:; @等。一种方法是使用switch case语句,如下所示。 but thats going to be simple and long process.
但那将是简单而漫长的过程。 Is there any other method short method possible?
有没有其他方法可用短方法?
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
fp = fopen("input.txt","r");
int ch,count[36]= {0};
if (fp == NULL)
{
fprintf(stderr,
"Failed to open input.txt: %s\n",
strerror(errno));
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
switch (ch)
{
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
default:
count[2]++;
}
}
fclose(fp);
}
printf("count a is %d", count[0]);
printf("count b is %d", count[1]);
printf("count c is %d", count[2]);
return 0;
}
In ASCII, printable characters have codes from 0x20
to 0x7E
, so less than 128 characters. 在ASCII中,可打印字符的代码为
0x20
至0x7E
,因此少于128个字符。 So for ASCII just use an array of 128 characters: 所以对于ASCII只使用128个字符的数组:
int count[128] = {0};
Update your count with: 更新您的计数:
count[ch]++;
and print printable characters with something like this: 并使用以下内容打印可打印字符:
for (i = 0x20; i <= 0x7E; i++)
{
printf("count %c is %d", i, count[i]);
}
Use an array of size 2^8 and increase the corresponding member. 使用大小为2 ^ 8的数组并增加相应的成员。
while ((ch = fgetc(fp)) != EOF)
{
characters[ ch ] += 1 ;
....
The index of the array characters
fits the asci table . 数组
characters
的索引适合asci表 。
if you are reading ASCII characters: 如果你正在读ASCII字符:
frequency[ch]++;
频率[CH] ++;
where frequency is integer array of size 128 其中frequency是大小为128的整数数组
If you use the functions from <ctype.h>
( isalpha
, isdigit
, ispunct
, etc) in a series of if
statements inside your while
loop, you could categorize them fairly easily. 如果在
while
循环中的一系列if
语句中使用<ctype.h>
( isalpha
, isdigit
, ispunct
等)中的ispunct
, if
可以相当容易地对它们进行分类。
PS: for a list of these functions, see: PS:有关这些功能的列表,请参阅:
http://www.cplusplus.com/reference/cctype/ http://www.cplusplus.com/reference/cctype/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.