Friday, 26 January 2018

Write a program to count number of characters, words and lines from a given input file. System Programming GTU 2150708



Program:-

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
            int coc=0,cow=0,col=0;
            ifstream f1;
            char ch;
            f1.open("newfile.txt");
            while(!f1.eof())
            {
                        f1.get(ch);
                        cout<<ch;
                        coc++;
                        if(ch=='  ' || ch=='\n')
                        {
                                    cow++;
                        }
                        if(ch=='\n')
                        {
                                    col++;
                        }
            }
            cout<<"\n Count of characters: "<<coc-2;
            cout<<"\n Count of Words: "<<cow-1;
            cout<<"\n Count of Lines: "<<col-1;
}



The file contains:

Hi
How are you doing
Good




Output:-

[root@SAR118 Desktop]# gcc prog1.cpp
[root@SAR118 Desktop]# ./a.out
[root@SAR118 Desktop]#
Count of characters: 23
Count of Words: 6
Count of Lines: 3

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...