this program demonstrates your how you can open a file and count number of character in that file and no of vowels in that file .....
#include<stdio.h>
#include<conio.h>
main()
{
//declare a file pointer
FILE *fp;
//declare a variable character type "ch" to store a character when read a file
char ch;
//declare integer variable "nov" to count no of vowels and "noc" to count number of character in file
int nov=0,noc=0;
clrscr();
//open file name x1.c
fp=fopen("x1.c","r");
//start reading file till end
while(1)
{
//get a single character in ch variable
ch=fgetc(fp);
//check for end of file
if(ch==EOF)
//exit while loop if file reached to end.
break;
//add 1 noc
noc++;
//get the variable in ch and check
switch(ch)
{
//add +1 to nov if ch is a e i o or u .....
case'a':
nov++;
break;
case'e':
nov++;
break;
case'i':
nov++;
break;
case'o':
nov++;
break;
case'u':
nov++;
break;
default:
continue;
}
}
//close file
fclose(fp);
//print number of character in file
printf("\n number of characters=%d",noc);
//print number of vowels in file
printf("\n number of vowels=%d",nov);
//wait for your key response
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
//declare a file pointer
FILE *fp;
//declare a variable character type "ch" to store a character when read a file
char ch;
//declare integer variable "nov" to count no of vowels and "noc" to count number of character in file
int nov=0,noc=0;
clrscr();
//open file name x1.c
fp=fopen("x1.c","r");
//start reading file till end
while(1)
{
//get a single character in ch variable
ch=fgetc(fp);
//check for end of file
if(ch==EOF)
//exit while loop if file reached to end.
break;
//add 1 noc
noc++;
//get the variable in ch and check
switch(ch)
{
//add +1 to nov if ch is a e i o or u .....
case'a':
nov++;
break;
case'e':
nov++;
break;
case'i':
nov++;
break;
case'o':
nov++;
break;
case'u':
nov++;
break;
default:
continue;
}
}
//close file
fclose(fp);
//print number of character in file
printf("\n number of characters=%d",noc);
//print number of vowels in file
printf("\n number of vowels=%d",nov);
//wait for your key response
getch();
}
No comments:
Post a Comment