C++ Goals Problem solving and debugging. Function decomposition. C++ strings. Do
ID: 3815319 • Letter: C
Question
C++ Goals
Problem solving and debugging.
Function decomposition.
C++ strings.
Documentation of program.
A text file contains a series of "words" that are to be reformatted.
Design a C++ program that when given the file (via Linux redirection) will do the following
count the number of non-whitespace characters found in the file (before words are reformatted)
count the number of vowels (upper and lower case) found in the file
count the number of letters (upper and lower case) found in the file
count the number of digits found in the file
for each "word" read,
convert all letters to lower case
remove any leading characters that are not letters or digits
remove any trailing characters that are not letters or digits
display the formatted words (to the screen) in 4 columns (left justified)
determine the longest words in the file (before the leading/trailing characters have been removed and after)
compute the average length of the formatted words (after the leading/trailing characters have been removed)
display the counts, the longest words, and the average length of the formatted words with labels (do not set precision for average, leave at least one blank line between end of table and start of counts)
REQUIREMENTS
The program MUST make use of functions to solve the problem. Identify subtasks and use functions to implement them. Minimum of 3 meaningful functions (in addition to main).
Arrays and/or vectors and/or structs and/or classes CANNOT be used in this program.
DO NOT use global variables. DO NOT use goto statements.
Assumptions/Definitions
a "word" is any series of consecutive non-whitespace characters
sample words before reformatting:
world, (2008) %#we're non-whitespace<+!> <{([r2d2])}>!!!
each word in the file will contain at least one letter or one digit
after the word is reformatted it must start and end with a letter or digit
sample reformatted words:
world 2008 we're non-whitespace r2d2
the "words" in the file will be separated by at least one blank space
each line in the file, including the last line of the file, will be terminated by a linefeed
the following letters will be considered vowels: A, E, I, O, U, a, e, i, o, u
maximum length of a word to be displayed will be 15 characters
the longest words in the file (both before and after formatting) will be unique
the input file will not be empty
Test your program adequately!
Documentation
When the program compiles and runs correctly, add the following documentation (comments) to your source file (.cpp file).
When a named constant and/or major variable is declared, provide a meaningful description of what it represents in the program.
For each function, clearly state what will be passed into the function and what will be passed out or returned by the function. Document important local variables. (See function documentation handout.)
//Include the Headers
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Function to count the words
int FindCount(string Words)
{
//returns the size
int it;
return Words.size();
}
//FUnction to find the vowels
int FindVowels(string Words)
{
//Local variables
char tempo;
int CounterVowels = 0;
int CounterConsonants = 0;
//for loop
for (int it = 0; it < Words.size(); it++)
{
//calls toupper()
tempo = toupper(Words[it]);
switch(tempo)
{
//cases
case 'A' : case 'a' :
case 'E' : case 'e' :
case 'I' : case 'i' :
case 'O' : case 'o' :
case 'U' : case 'u' :
//Increments the vowel count
CounterVowels++;
break;
default:
//Checks the counter consonants
CounterConsonants++;
}
}
//Returns the vowels
return CounterVowels;
}
//Function to find the words
int FindLetters(string Words)
{
int lt = 0;
//for loop
for (int it = 0; it < Words.size(); it++)
{
//calls isalpha()
if(isalpha(Words[it]))
lt++;
}
//Returns the letter
return lt;
}
//Function to find the digits
int FindDigits(string Words)
{
int lt = 0;
//for loop
for (int it = 0; it < Words.size(); it++)
{
//calls the isDigit()
if(isdigit(Words[it]))
lt++;
}
return lt;
}
//Function to convert the words
string ConvertWords(string words)
{
int it=0;
//For loop
for(it=0;it<words.size();it++)
{
//Assigns the words
words[it] = tolower(words[it]);
}
//Returns the words
return words;
}
//function to do removal()
string LeadsRemoval(string Words)
{
int it=0, posi=0, flags=0;
do
{
//checks the digits and alpha
if(isdigit(Words[it]) || isalpha(Words[it]))
{
//Assigns the flag as 1
flags = 1;
}
//else statement
else
{
//for loop
for(posi=0;posi<Words.size();posi++)
{
//checks the length
Words.length()-1;
char c= Words[posi];
//position
Words[posi] = Words[posi+1];
}
}
}while(flags==0);
//returns the flags
return Words;
}
//rogram starts with main
int main()
{
string NameFile;
string words1, Words, w_longest = " ", w_longest1 = " ";
int it=0, countt=0, vo=0, lt=0, gtt=0;
//gets the file name
getline(cin,NameFile);
cout<<" Word list ";
//checks the file input
if (NameFile)
{
while (NameFile >> words1)
{
//calls the LeadsRemoval()
string w = LeadsRemoval(words1);
cout << ConvertWords(w) <<" ";
it++;
//calls the function
countt = countt+FindCount(words1);
vo = vo+FindVowels(words1);
lt = lt+FindLetters(words1);
gtt = gtt+FindDigits(words1);
//checks the size
if(words1.size()>w_longest.size())
//Assigns the words
w_longest = words1;
//checks the size
if(w.size()>w_longest1.size())
//Assigns the words
w_longest1 = w;
if(it%4==0) cout<<" ";
}
//statement to print
cout<<" Non-Whitespace characters : "<<countt;
cout<<" Vowels : "<<vo;
cout<<" Letters : "<<lt;
cout<<" Digits : "<<gtt;
cout<<" Longest Words before formatting : "<<w_longest;
cout<<" Longest Words after formatting : "<<w_longest1;
cout<<" Average length of formatted words : "<<(w_longest.size()+w_longest1.size())/2;
//file close
fileInput.close();
}
return 0;
}
The output only shows:
Word List:
Explanation / Answer
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char c,y;
char fname[10];
clrscr();
ofstream out;
cout<<"Enter File Name:";
cin>>fname;
out.open(fname);
cout<<"Enter the text(Enter # at end) ";
while((c=getchar())!='#')
{
y=c-20;
out<<y;
}
out.close();
ifstream in(fname);
cout<<" The File contains ";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.