#include <stdio.h>
#include <ctype.h>
#define file "count.c"
int main()
{
FILE *fs;
// Data required
int Ualpha[26]={0};
int Lalpha[26]={0};
int nums[10]={0};
int i;
fs = fopen(file,"r");
if(fs == NULL)
{
printf("Error");
exit(0);
}
char ch;
// Scan char by char and check for type
while( (ch=getc(fs)) != EOF )
{
if(isupper(ch))
Ualpha[ch-'A'] += 1; // count UpperCases
else if(islower(ch))
Lalpha[ch-'a'] += 1; // Count LowerCases
else if(isdigit(ch))
nums[ch-'0'] += 1; // Count numerics
}
// print Uppercase counts
for(i=0 ; i<26 ;i++)
printf("%c ",'A'+i);
printf("\n");
for(i=0 ; i<26 ;i++)
printf("%d ",Ualpha[i]);
printf("\n\n\n");
// print Lowercase counts
for(i=0 ; i<26 ;i++)
printf("%c ",'a'+i);
printf("\n");
for(i=0 ; i<26 ;i++)
printf("%d ",Lalpha[i]);
printf("\n\n\n");
// print digits counts
for(i=0; i<10 ; i++)
printf("%d ",i);
printf("\n");
for(i=0; i<10 ; i++)
printf("%d ",nums[i]);
return(0);
}
Programming takes discipline. Good programming takes a lot of discipline, a large number of principles, and standard, defensive ways of doing things right. No programming technique solves all problems. No programming language produces only correct results. No programmer should start each project from scratch
Saturday, February 16, 2013
Count Upper, Lower case alphabets and digits in a file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment