Count and display number of charcters, white spaces, tabs, new lines present in the content of file - C

Q. Write a program to read content of data file named "PU" and count and display number of characters, white spaces, tab spaces and new lines present in the content of file. 

#BIT-FirstSem-2018-GroupB-7No-8Marks-Solution
==> code goes like this:

#include<stdio.h>
int main()
{
    FILE *fptr;
    char ch;
    int character=0, space=0, tab=0, newline=0;
    fptr = fopen("D:\\PU.dat", "r");
    if(fptr==NULL)
    {
        printf("\n File can't be opened");
        exit(1);
    }
    while(fptr)
    {
        ch = fgetc(fptr);
        if(ch==EOF)
        {
            break;
        }
        character++;
        if(ch==' ')
        {
            space++;
        }
        if(ch=='\t')
        {
            tab++;
        }
        if(ch=='\n')
        {
            newline++;
        }
    }
    fclose(fptr);
    printf("\nNumber of characters = %d", character);
    printf("\nNumber of spaces = %d", space);
    printf("\nNumber of tabs = %d", tab);
    printf("\nNumber of newline = %d", newline);
    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