Display number of vowels and white spaces from the given text stored in a data file - C

Q. Write a program to to input a line of text and store it in a data file and display
number of vowels and white spaces from the given text stored in a data file.
#BIT_FirstSem_2016_7No_8Marks_Solution
==> Code goes like this:

#include <stdio.h>
int main()
 {
          FILE *fptr;
          int vowel=0,space=0;
          char c,ch;
          //Writing into file
          fptr=fopen("D:\\data.dat","w+");
          if(fptr==NULL)
          {
              printf("\n File can't be created");
              exit(1);
          }
          printf("\n Write some text and hit Enter key : ");
          fflush(stdin);
          while ((c=getchar())!='\n')
          {
              fputc(c,fptr);
          }
          fclose(fptr);
          //reading from file
          fptr=fopen("D:\\data.dat","r");
          if(fptr==NULL)
            {
                    printf("Source can't be opened");
                    exit(0);
            }
          ch=fgetc(fptr);
          while(ch!=EOF)
                {
                       if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch=='I')||(ch=='o') ||(ch=='O')||(ch=='u')||(ch=='U'))
                         {
                               vowel++;
                          }
                          if(ch==' ')
                          {
                              space++;
                          }
                         ch=fgetc(fptr);
                }
       fclose(fptr);
       printf("\n Number of vowels are: %d \n",vowel);
       printf("\n Number of space is: %d", space);
       return 0;
}

Output:








If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment