Question

In: Computer Science

SpeechAnalyst The purpose of this assignment is to practice using strings and C-strings. Your task is...

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

SpeechAnalyst( );

void clear( ); // resets everything...
void addData( char * stuff );
void addStringData( std::string stuff );

int getNumberOfWords( ) const;
int getNumberOfSentences( ) const;

friend ostream& operator << ( ostream& outs, const SpeechAnalyst & sa ); // prints the data seen so far!

std::string myData;

SpeechAnalyst sa;
cout << sa << endl;
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." );
sa.addStringData( speech );
cout << sa << endl;

sa.clear();
char * data = new char[ 50 ];
strcpy( data, "Muffin says Hello." );
sa.addData( data );
cout << sa << endl;
sa.addData( data );
cout << sa << endl;

Sample Output

No data to print out....
Data has 29 words and 1 sentence.
Data has 3 words and 1 sentence.
Data has 6 words and 2 sentences.

HINT: You may add additional methods to the SpeechAnalyst class as long as you implement those defined in the original class definition.

Solutions

Expert Solution

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;

}


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Objectives: Practice using files and strings. More practice using loops with files and strings Background Assume...
Objectives: Practice using files and strings. More practice using loops with files and strings Background Assume you're working on a contract where the company is building a e-mailing list. Your task is to write a C++ program that reads through an e-mail stored in the current working directory, called mail.dat, and outputs every email address found as individual lines to a file called email_list.dat. For this assignment, if a whitespace delimited string of characters has an embedded commercial at sign...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Policy Drivers The purpose of this assignment is to practice and demonstrate your ability to interpret...
Policy Drivers The purpose of this assignment is to practice and demonstrate your ability to interpret detailed policy. We have chosen for you to take a look at two of the most well known policies; in real life, you will have government polices such as these as well as enterprise specific policies or regulations. As you build information systems, it is key to early on in the process to identify all relevant policy drivers and understand them. In the module,...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data sets to apply the concepts of sampling distributions and confidence intervals to make management decisions. Assignment Steps Resources: Microsoft Excel®, The Payment Time Case Study, The Payment Time Case Data Set Review the Payment Time Case Study and Data Set. Develop a 700-word report including the following calculations and using the information to determine whether the new billing system has reduced the mean bill...
in c++ language This assignment is to give you practice using structs and sorting. In competitive...
in c++ language This assignment is to give you practice using structs and sorting. In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores, adding the remaining scores, and then multiplying...
Using the Payback Method, IRR, and NPV Problems Purpose of Assignment The purpose of this assignment...
Using the Payback Method, IRR, and NPV Problems Purpose of Assignment The purpose of this assignment is to allow the student to calculate the project cash flow using net present value (NPV), internal rate of return (IRR), and the payback methods. Assignment Steps Resources: Corporate Finance Calculate the following time value of money problems in Microsoft Excel or Word document. You must show all of your calculations. If you want to accumulate $500,000 in 20 years, how much do you...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with arrays and array parameters. Arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot operator ( .), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT