Problem solving and debugging. Linux redirection to read data from a file. Use o
ID: 3844259 • Letter: P
Question
Problem solving and debugging.
Linux redirection to read data from a file.
Use of conditional loop (while).
Data File Description
A data file for this exercise will consist of several lines of text that form sentences. The text may include letters, punctuation marks, and/or digits. Each sentence will be terminated with either a period (.), an exclamation mark (!), or a question mark (?).
Design and implement a complete C++ program that will
using Linux redirection, read the contents of the file and
compute the average number of letters per sentence in the file
count the number of digits in the file
display the average with a label and 3 digits to the right of the decimal
display the digit count with a label
NOTES:
Make sure you choose enough test data to ensure that your program meets all the requirements.
If you use any library functions in your program make sure you include the appropriate header file(s).
Sample terminal session:
[keys]$ more data4eight
What is Earth Day? Earth Day is
celebrated annually on April 22 to demonstrate support for
environmental protection. It was first marked
in 1970 and is coordinated globally by the
Earth Day Network. More than
193 countries participate each year.
[keys]$ g++ ex08.cpp
[keys]$ ./a.out < data4eight
Average # of letters per sentence is 48.250
Total # of digits in the file is 9
**EXTRA NOTES:
-This does NOT use fstream. Input will be taken through cin.
-String cannot be used. The program can be created without it.
Explanation / Answer
As per your requirement, I have developed the C++ Program which will first explain and brief you about the spirnt one i.e, Each sentence will be terminated with either a period (.), an exclamation mark (!), or a question mark (?). I have atttached Two files with .txt extensions for each and specific criteria. Compile both the files and execute it.
Basically, It invloves all the code with the comments for each line of code for the better undertstanding of the program and makes it gain practical knowledge.
In essence, It elaborates more on average number of letters per sentence in the file and next count the number of digits in the file.
First Program:-
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int wrdCounts(string sentences);
int ltrCounts(string sentences);
int main()
{
string sentenceString;
cout << "Type the words to check the Average of each sentences:-" << endl;
getline(cin, sentenceString);
int wdCont = wrdCounts(sentenceString);
cout << " The Sentence has " << wdCont << " words." << endl;
int ltrCont = ltrCounts(sentenceString);
cout << " There are " << ltrCont <<" letters in the entered sentence." << endl;
double average = 0;
if(wdCont > 0) average = double(ltrCont)/wdCont;
cout << " The average number of letters per word is " << average
<< " Press 'Enter' to continue ... " << flush;
cin.get();
return 0;
}
// Gives the No. of letters in the sentences
int ltrCounts(string sentences)
{
int letters = 0;
for(unsigned count = 0; count < sentences.length(); count++)
if(isalpha(sentences[count]))
letters++;
return letters;
}
// Gives the No. of words in the sentences
int wrdCounts(string sentences)
{
int sLen = sentences.length();
if(sLen == 0)
return 0;
int words = 1;
for(int i = 0; i < sLen; i++)
if(sentences[i] == ' ' && sentences[i+1] != ' ')
words++;
if(sentences[0] == ' ')
words--;
if(sentences[sentences.length()-1] == ' ')
words--;
return words;
}
/* Count No. of Digits Program */
#include <iostream>
using namespace std;
int main()
{
string line;
int digits;
digits = 0;
std::string word;
std::cout << "Enter the words to check the end of punctuation marks:- ";
getline (std::cin, word);
std::cout << "" << word << ". " || "! " || "? ";
cout << "Enter a line of sentence: ";
getline(cin, line);
for(int i = 0; i < line.length(); ++i)
{
if(line[i]>='0' && line[i]<='9')
{
++digits;
}
}
cout << "Digits: " << digits << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.