In: Computer Science
SpeechAnalyst
The purpose of this assignment is to practice using strings and
C-strings. Your task is to create a class that counts words (by
looking for spaces) and sentences (by looking for periods). The
data will either come in dribs-and-drabs. Use the infamous operator
"<<" to output the data seen so far. Because
std::string is a class, I'd recommend you use it
to store the text information that has been entered through calls
to addData and addStringData.
|
Implementation Details |
Sample Driver |
|||
|
SpeechAnalyst sa; sa.clear(); |
|||
|
Sample Output |
||||
|
No data to print out.... |
HINT: You may add additional methods to the SpeechAnalyst class as long as you implement those defined in the original class definition.










Copyable code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
//class defintion
class SpeechAnalyst
{
//Access spicifier
public:
//declare the "SpeechAnalyst()" functions
SpeechAnalyst();
//declare the "clear()" functions
void clear();
//declare the "addData()" functions
void addData(char* stuff);
//declare the "addStringData()" functions
void addStringData(std::string stuff);
//declare the "getNumberOfWords()" functions
int getNumberOfWords() const;
//declare the "getNumberOfSentences()" functions
int getNumberOfSentences() const;
//declare the functions
friend ostream& operator <<(ostream& outs, const SpeechAnalyst & sa);
//Access spicifier
private:
std::string myData;
};
//function definition of "SpeechAnalyst()"
SpeechAnalyst::SpeechAnalyst()
{
myData = "";
}
//function definition of "clear()"
void SpeechAnalyst::clear()
{
myData = "";
}
//function definition of "addData()"
void SpeechAnalyst::addData(char* stuff)
{
//if the value of "myData.length()" is greater than 0 increment the "myData"
if(this->myData.length() > 0)
//increment the "myData"
myData += " ";
//check for the condition
while(*stuff++ != '\0')
{
//calculate the "myData" value
myData += *stuff;
}
}
//function definition of "addStringData()"
void SpeechAnalyst::addStringData(std::string stuff)
{
//if the value of "myData.length()" is greater than 0 increment the "myData"
if(this->myData.length() > 0)
//increment the "myData"
myData += " ";
//calculate the "myData" value
myData += stuff;
}
//function definition of "getNumberOfWords()"
int SpeechAnalyst::getNumberOfWords() const
{
//declare the "x" variable as an integer data type
int x = 0;
//check for the condition
for(std::string::size_type i = 0; i < this->myData.length(); i++)
{
//if the value of "myData.length()" is equal to null increment the "x"
if((this->myData).at(i) == ' ')
//increment the "x"
x++;
}
//return the "x+1" value
return x + 1;
}
//function definition of "getNumberOfSentences()"
int SpeechAnalyst::getNumberOfSentences() const
{
//declare the "y" variable as an integer data type
int y = 0;
//check for the condition
for(std::string::size_type i = 0; i < this->myData.length(); i++)
{
//if the value of "myData.length()" is equal to null increment the "y"
if ((this->myData).at(i) == '.')
//increment the "y"
y++;
}
//return the "y" value
return y;
}
//function definition
ostream& operator <<(ostream& outs, const SpeechAnalyst& sa)
{
//if the value of "myData.length()" is greater than 0 display the data
if(sa.myData.length() > 0)
{
//display the data
outs << "Data has " << sa.getNumberOfWords();
//if the value of "getNumberOfSentences()" is equal to 1 display the number of words
if(sa.getNumberOfSentences() == 1)
// display the number of words
outs << " words and ";
//otherwise
else
// display the number of words
outs << " words and ";
//display the output
outs << sa.getNumberOfSentences();
//if the value of "getNumberOfSentences()" is equal to 1 display the sentences
if(sa.getNumberOfSentences() == 1)
//display the number of sentences
outs << " sentence.";
//otherwise
else
//display the number of sentences
outs << " sentences.";
}
//otherwise
else
//display the output
outs << "No data to print out....";
//return the "outs"
return outs;
}
//main method
int main()
{
//create the object for the class "SpeechAnalyst"
SpeechAnalyst sa;
//display the content
cout << sa << endl;
//set the paragraph
std::string speech("Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.");
//call the "addStringData" function with the argument "speech"
sa.addStringData(speech);
//display the content
cout << sa << endl;
//call the "clear" function
sa.clear();
//declare the variable "data" as a character data type
char* data = new char[50];
//copy the string
strcpy(data, "Muffin says Hello.");
//call the "addStringData" function with the argument "data"
sa.addData(data);
//display the content
cout << sa << endl;
//call the "addStringData" function with the argument "data"
sa.addData(data);
//display the content
cout << sa << endl;
return 0;
}