Okej, my first homework is to open a file for reading and count all the letters from a-z, and the capitals and small are the same. So 'a' and 'A' is the same. I have to count how many of each letters are in this file.
This is my codebut didn't include for all the letters)
Code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int br;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,r,s,t,u,v,z=0;
FILE *datoteka;
if(argc<2)
{
printf("Napisati morate argument-ime datoteke!");
exit(1);
}
datoteka=fopen(argv[1],"r");
while ((br=fgetc(datoteka)) !=EOF)
{
switch(br)
{
case 'a':
case 'A': a++;
break;
case 'b':
case 'B': b++;
break;
case 'c':
case 'C': c++;
break;
case 'd':
case 'D': d++;
break;
case 'E':
case 'e': e++;
break;
case 'F':
case 'f': f++;
break;
case 'g':
case 'G': g++;
break;
case 'h':
case 'H': h++;
break;
case 'i':
case 'I': i++;
break;
case 'j':
case 'J': j++;
break;
case 'k':
case 'K': k++;
break;
case 'l':
case 'L': l++;
break;
case 'm':
case 'M': m++;
break; //and so on to letter Z
}
}
fclose(datoteka);
printf("A:%d B:%d C:%d ....and so on\n",a,b,c);
return 0;
}
Now, the above code does the job, but I have this feeling that this can be done with a lot less code. Any suggestions?