C++ please Specifications: The text file is called \"bob.txt\", and can be found
ID: 3596869 • Letter: C
Question
C++ please Specifications: The text file is called "bob.txt", and can be found on the website for ECE 71. It contains the lyrics for Weird Al Yankovic's palindrome song. "Bob" in which every line is composed of a palindrome https://www.voutube.com/watch?v JUODzi6R3 Copy and paste the file into a "bob.txt" file within your project directory, in a manner similar to HW 20 Use a function to read in each character one at a time from the file, following the rules noted above to determine whether the character should be counted toward a word or not. Only count characters that are part of words, and be sure to increase the word count only for a valid word. It will be helpful to keep track of the current character and previous character. The function should also work with the stream cin as an input stream, even though the function will not be called with cin as an argument in this program. Use the following function prototype void wordLength (istream &bob;) Print 3 columns to the screen. As characters are read in from the file, the first column will print every character in the file. The second column will print the current valid character count. The third columrn will print the current number of words. Separate each column with the tab character. After the entire file has been printed, calculate and print the average number of characters per word using 2 values after the decimal point. of the main function is to open the file, exit the program i f the file fails to open, cal The only purpose the wordLength ) function, and close the file As an example, if you execute the program with the following underlined inputs, the output will be:
Explanation / Answer
#include<iostream>
#include<string.h>
#include <ctype.h>
#include <fstream>
using namespace std;
void wordLength(istream &is)
{
int char_Counter=0, word_Counter=0;
char ch;
char prev_ch='';
while(!is.eof())
{
is>>ch;
if( isalpha(ch) )
char_Counter++;
if( prev_ch !='' && isalpha(prev_ch) && !isalpha(ch)) //increment the char count if current char is alphabate
word_Counter++;
cout<<ch<<" Chars:"<<char_Counter<<" Words:"<<word_Counter<<endl; //increment the word count if prev char was alphabate and current char is non alphabate
prev_ch=ch;
}
if(is.eof() && isalpha(ch)) //counting the last word if not counted.
word_Counter++;
cout<<" "<<" Chars:"<<char_Counter<<" Words:"<<word_Counter<<endl;
}
int main()
{
string filename;
cout<<"Enter the filename";
cin>>filename;
ifstream is;
is.open(filename.c_str());
if(is.fail())
{
cerr << "Error opening input file ";
return 1;
}
wordLength(is);
getchar();
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.