As a test prep I have to analyze the body text of a large book, which is being r
ID: 3686094 • Letter: A
Question
As a test prep I have to analyze the body text of a large book, which is being retrieved from the Gutenberg project: http://www.gutenberg.org/cache/epub/18209/pg18209.txt
Since the file is in plain text and has a large number of lines, the contents can be views opening the file with either Notepad or Wordpad. As a test prep how would you write a C++ program that finds out:
1. The number of lines in the text
2. The total number of characters (including, number, letters, symbols, etc.)
3. The total number of letters
4. The total number of capital letters
5. The number of characters of the longest line in the file
6. The largest word in the file.
Just the cpp file. Hint: Use the “getline()” function. (C++ programming)
Explanation / Answer
#include<iostream.h>
#include<stdlib.h>
using namespace std;
int main()
{
const int length = 1000;
char text[length] = {0};
cout<<"Enter few words: ";
cin.getline(text,length);
cout<<"You Have Entered: "<<text<<endl;
int space=0;
int vow=0;
int consonants=0;
int special=0;
int ucase=0;
int lcase=0;
int intr=0;
int line=0;
for(int i = 0 ; text[i] != '' ; i++)
{
if(isalpha(text[i]))
{
if(text[i]>=65 && text[i]<=90)
{
ucase++;
}
else if(text[i]>=97 && text[i]<=122)
{
lcase++;
}
switch(tolower(text[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vow++;
break;
default:
consonants++;
break;
}}
else if(text[i]==' ')
{
space++;
}
else if((text[i]>=0&&text[i]<=47)||(text[i]>=58&&text[i]<=64)||(text[i]>=91&&text[i]<=96)||(text[i]>=123&&text[i]<=127))
{
special++;
}
else if(text[i]>=48 && text[i]<=57)
{
intr++;
}
else if(text[i]==' ')
{
line++;
}
}
cout<<"No. of vowels="<<vow<<endl;
cout<<"No. of consonants="<<consonants<<endl;
cout<<"No. of spaces="<<space<<endl;
cout<<"No. of words="<<space+1<<endl;
cout<<"No. of special letters="<<special<<endl;
cout<<"No. of upper case letters="<<ucase<<endl;
cout<<"No. of lower case letters="<<lcase<<endl;
cout<<"No. of integers="<<intr<<endl;
cout<<"No. of lines="<<line;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.