Retrieve Hamlet from the Gutenberg project and save it as a plain text file: The
ID: 3567399 • Letter: R
Question
Retrieve Hamlet from the Gutenberg project and save it as a plain text file: The file consists of a large number of lines. Write a C++ program making use of dynamic memory and pointers to find out how many of these lines contain an even number of letters and how many an odd number of letters regardless of their case. Only consider letters, so skip counting punctuation marks, numbers, spaces, etc. For example, the line: Claudius King of Denmarke. has 22 letters, which is an even number, so it should add one to the count of even lines. Print the calculated number of even and odd counts after processing the whole file.
Explanation / Answer
Here is your desired code
provide file Name (path/fileName) to the Variable "FilePath" and enjoy..!
Best Regards,
Mutafaf
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//here in file path provide your file path
char filePath[]="file.txt";
ofstream outStream;
outStream.open(filePath,ios::app);
outStream<<endl;
outStream.close();
ifstream inStream;
inStream.open(filePath);
int charCount=0;
int evenCount=0;
int oddCount=0;
char c; //character to check
while(inStream.get(c))
{
if((c>='A' && c<='Z') || (c>='a' && c<='z'))
{
charCount++;
}
if(c==' ' && charCount!=0)
{
if(charCount%2==0)
{
evenCount++;
}
else
{
oddCount++;
}
charCount=0;
}
}
cout<<"Lines with even number of characters: "<<evenCount;
cout<<" Lines with odd number of characters: "<<oddCount;
inStream.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.