It counts upper lower and digits in a file ..
Count Upper, Lower case alphabets and digits in a file
#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);
}
Narue 5,707 Bad Cop Team Colleague
Shankye 36 Junior Poster
Narue 5,707 Bad Cop Team Colleague
Shankye 36 Junior Poster
vinitmittal2008 37 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.