- ahmad alhayek

 in c++

Write a C++ program that reads a file named words.txt and counts and prints the total number of words, the total number of VI
Sample Testcase 0: (To try on your computer, it wont be graded, there is another secret text). Input: Suppose words.txt has
1 include <iostream> 2. #include <fstream> 3 #include <string> 4 #include <iomanip> 5 using namespace std; 6 + int mainot 8 9
Show transcribed image text

Expert Answerinformation icon

  • Anonymous's Avatar

    NOTE:- IN CASE OF ANY QUERY FEEL FREE TO ASK IN COMMENT ANYTIME.........HAPPY LEARNING AND KEEP CHEGGING.....

    code

    #include<iostream>
    #include<fstream>
    using namespace std;
     
    int main()
    {
     ifstream fin("word.txt"); //opening a file
     int line=1,word=0; //assign values to line and word 
     char ch;
     int character=0,space=0; //character and space set to 0 
     
     fin.seekg(0,ios::beg); //move  to begining of file
     
     while(fin)
     {
      fin.get(ch); //get every character 
      if(ch==' '||ch=='\n') //new line or space encounterd
       word++;
       
      if(ch=='\n') //new line 
       line++;
    if(!(ch==' '|| ch=='\n')) //not space or not a new line then its a character 
            character++;
                    
    if(ch== ' ') //space 
            space++;
     } //display result 
     cout<<"Number of characters without spaces:\t"<<character-1;
     cout<<"\nNumber of characters with spaces:\t"<<character+space-2;
     cout<<"\nNumber of words:\t\t\t"<<word;
     cout<<"\nNumber of lines:\t\t\t"<<line;
     cout<<"\nSpaces count:\t\t\t\t"<<space-1;
     
     fin.close(); //closing file
     
     return 0;
     
    }

    code image

    8 1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 5 int main() 6- { 7 ifstream fin(word.txt); //opening a

    output

    351 409 Number of characters without spaces: Number of characters with spaces: Number of words: Number of lines: Spaces count

    --------------I hope my answer met all your requirements......Thank You-----------

    Comment 

ليست هناك تعليقات:

إرسال تعليق

ahmad alhayek تصميم ahmad alhayek جميع الحقوق محفوظة 2016

صور المظاهر بواسطة sndr. يتم التشغيل بواسطة Blogger.