Question

Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

Please add a detailed comment for this program.

#include<iostream>

#include<string>

#include<fstream>

#include<sstream>

#include<cctype>

using namespace std;

int is_palindrome(string word){

int len = word.size();

for(int i=0; i<len/2; i++){

if(toupper(word[i])!=toupper(word[len-i-1]))

return 0;

}

return 1;

}

int have_vowels3(string word){

int cnt = 0;

for(int i=0; i<word.size(); i++){

if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u')

cnt++;

}

if(cnt>=3)

return 1;

else

return 0;

}

int have_consecutives(string word){

for(int i=0; i<word.size()-1; i++){

if(tolower(word[i])=='o' && tolower(word[i+1]=='o'))

return 1;

}

return 0;

}

int main(){

ifstream in;

in.open("speech.txt");

if(in.fail()){

cout<<"Unable to open file"<<endl;

return 0;

}

int no_lines(0), no_words(0), no_chars(0), no_palin(0), no_vowels(0), no_consec(0);

string line;

while(!in.eof()){

no_lines++;

getline(in, line);

istringstream ss(line);

string word;

while(ss>>word){

no_words++;

no_chars += word.size();

int isPalin = is_palindrome(word);

int haveVow = have_vowels3(word);

int haveConsec = have_consecutives(word);

no_palin += isPalin;

no_vowels += haveVow;

no_consec += haveConsec;

}

}

cout<<"Total number of lines: "<<no_lines<<endl;

cout<<"Total number words: "<<no_words<<endl;

cout<<"Total number of characters in a text file: "<<no_chars<<endl;

cout<<"Total number of one word palindrome: "<<no_palin<<endl;

cout<<"Total number of words that have 3 vowels in a row: "<<no_vowels<<endl;

cout<<"Total number of words that have 2 consecutive o¡¯s: "<<no_consec<<endl;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<string>

#include<fstream>

#include<sstream>

#include<cctype>

using namespace std;

// check fi the string passed as argument is palindrome

int is_palindrome(string word)

{

    // get the size of word

    int len = word.size();

   

    // traverse through the half of string

    for(int i=0; i<len/2; i++)

    {

        // if the i th character from start and end are not same

        // it is not palindrome

        if(toupper(word[i])!=toupper(word[len-i-1]))

            return 0;

    }

   

    // if the string is palindrome

    return 1;

}

// check if no of vowels is greater than equal to 3

int have_vowels3(string word)

{

    // store the number of vowels  

    int cnt = 0;

   

    // traverse the word string

    for(int i=0; i<word.size(); i++)

    {

        // tolower() makes the character into lower case

        // if the current character is vowel

        if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u')

            cnt++;

    }

   

    // if no of vowels is greater than equal to 3

    if(cnt>=3)

        return 1;

    // if no of vowels is less than 3

    else

        return 0;

}

// check if string passed as argument contains consecutive 'o'

int have_consecutives(string word)

{

    for(int i=0; i<word.size()-1; i++)

    {

        // if curren or next character is 'o'

        if(tolower(word[i])=='o' && tolower(word[i+1]=='o'))

            return 1;

    }

   

    // if no two consecutive character are 'o'

    return 0;

}

int main()

{

    ifstream in;

   

    // open file to read

    in.open("speech.txt");

   

    // if the function is unabl to open

    if(in.fail())

    {

        cout<<"Unable to open file"<<endl;

       

        // stop the program

       return 0;

    }

   

    int no_lines(0), no_words(0), no_chars(0), no_palin(0), no_vowels(0), no_consec(0);

   

    string line;

   

    // loop untill file ends

    while(!in.eof()){

       

        // increment the number of lines

        no_lines++;

       

        // read entire line from file

        getline(in, line);

       

        istringstream ss(line);

       

        string word;

       

        while(ss>>word){

       

            no_words++;

           

            no_chars += word.size();

           

            // check if word is palindrome

            int isPalin = is_palindrome(word);

           

            // check if word has more than 3 vowels

            int haveVow = have_vowels3(word);

           

            // check if word has consecutive 'o'

            int haveConsec = have_consecutives(word);

           

            no_palin += isPalin;

            no_vowels += haveVow;

            no_consec += haveConsec;

           

        }

   

    }

   

    cout<<"Total number of lines: "<<no_lines<<endl;

    cout<<"Total number words: "<<no_words<<endl;

    cout<<"Total number of characters in a text file: "<<no_chars<<endl;

    cout<<"Total number of one word palindrome: "<<no_palin<<endl;

    cout<<"Total number of words that have 3 vowels in a row: "<<no_vowels<<endl;  

    cout<<"Total number of words that have 2 consecutive o¡¯s: "<<no_consec<<endl;

   

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace...

    #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace std; void DisplayMenu() {    cout << "1. E games\n";    cout << "2. T games\n";    cout << "3. M games\n";    cout << "4. Total Games\n";    cout << "5. Exit\n"; } double total(double egames, double tgames, double mgames) {    int totalgames = egames + tgames + mgames;    cout << "There are " << totalgames << " games\n";    return...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...

    I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]);    if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT