Question: 8
Write a function in C named “count_digits()” to read the contents of a file, count and print total digits in the file?
#include<stdio.h> #include<conio.h> void count_digits() { FILE *fp; char ch,fname[15]; int a=0; printf("Enter name of the file to read "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) { printf("Unable to open the file\n"); getch(); return; } while((ch=getc(fp))!=EOF) { printf("%c",ch); if(ch>='0' && ch<='9') a++; } fclose(fp); printf("total digits = %d\n",a); } int main() { count_digits(); return(0); }
Question:9
Write a function in C named “count_vowels()” to read the contents of a file, count and print total vowels in the file?
#include<stdio.h> #include<conio.h> void count_vowels() { FILE *fp; char ch,fname[15]; int v=0; printf("Enter name of the file to read "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) { printf("Unable to open the file\n"); getch(); return; } while((ch=getc(fp))!=EOF) { printf("%c",ch); if(ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u) v++; } fclose(fp); printf("total vowels = %d\n",v); } int main() { count_vowels(); return(0); }
Question:10
Write a function in C named “count_spaces()” to read the contents of a file, count and print total spaces in the file?
#include<stdio.h> #include<conio.h> void count_spaces() { FILE *fp; char ch,fname[15]; int s=0; printf("Enter name of the file to read "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) { printf("Unable to open the file\n"); getch(); return; } while((ch=getc(fp))!=EOF) { printf("%c",ch); if(ch==' ') s++; } fclose(fp); printf("total spaces = %d\n",s); } int main() { count_spaces(); return(0); }
Question: 11
Write a function in C named “count()” to read the contents of a file, count and print total:
Alphabets
Lower case alphabets
Upper case alphabets
Digits
Spaces
Special characters
Length of the file
#include<stdio.h> #include<conio.h> void count() { FILE *fp; char ch,fname[15]; int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0; clrscr(); printf("Enter name of the file to read "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) { printf("Unable to open the file\n"); return; } while((ch=getc(fp))!=EOF) { printf("%c",ch); if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) { a++; if (ch>='A' && ch<='Z') ua++; else la++; } else if (ch>='0' && ch<='9') d++; else if(ch==' ') sp++; else spl++; i++; } fclose(fp); printf("total len %d\n",i); printf("Total alpha %d lower %d upper alpha %d\n",a,la,ua); printf("total digits %d spaces %d special char %d\n",d,sp,spl); } int main() { count(); return(0); }