Showing posts with label Count Character in file. Show all posts
Showing posts with label Count Character in file. Show all posts

Sunday, September 11, 2011

count the no. of characters and no. of vowels in a file 2

// to count the no. of characters & no. of vowels in a file

#include<stdio.h>
#include<conio.h>
main()
{
    FILE *fp;
    char ch;
    int nov=0,noc=0;
    clrscr();
    fp=fopen("b.c","r");  // here 'b' is the name of the file to be opened.
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
        break;
        noc++;
        if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
            nov++;

           }
        fclose(fp);
        printf("\n number of characters=%d",noc);
        printf("\n number of vowels=%d",nov);
        getch();
        }

count the no. of characters and no. of vowels in a file

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();
        }

Popular 5 Posts Last 7 days