The purpose of this assignment is to practice using strings

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.

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.

Solution

//Save as SpeechAnalyst.h
#ifndef SPEECHANALYST_H
#define SPEECHANALYST_H
#include<iostream>
#include<string>
using namespace std;
//SpeechAnalyst class declaration
class SpeechAnalyst
{
private:
   //private string data member
   std::string myData;

public:
   //public data members
   SpeechAnalyst();
   void clear();
   void addData(char * stuff);
   void addStringData(std::string stuff);
   int getNumberOfWords() const;
   int getNumberOfSentences() const;
   friend ostream& operator << (ostream& outs, const SpeechAnalyst & sa);
};

#endif SPEECHANALYST_H

--------------------------------------------------------------------------------

//Save as SpeechAnalyst.cpp
#include \"SpeechAnalyst.h\"
//constructor
SpeechAnalyst::SpeechAnalyst()
{
   //set MyData to empty
    myData=\"\";
}


//Method addData that takes character pointer
//add the content from the pointed by stuff
//to the myData until null character is encountered
void SpeechAnalyst::addData(char * chPtr)
{
    while (*chPtr++ != \'\\0\'){
        myData += *chPtr;
    }
}

/*
The method addStringData that takes string and set to myData
*/
void SpeechAnalyst::addStringData(std::string stuff)
{
    myData = stuff;
}

/**
The method getNumberOfWords that retunrs the number of words
*/
int SpeechAnalyst::getNumberOfWords() const
{
    int spaceCount = 0;
    for (int i = 0; i<(this->myData).length(); i++)
   {
        if ((this->myData).at(i) == \' \')
            spaceCount++;
    }
    return spaceCount + 1;
}


/**
The method getNumberOfSentences that cound number of sentences based
on the dot symbol.
*/
int SpeechAnalyst::getNumberOfSentences() const
{
    int numDots = 0;
    for (int i = 0; i<(this->myData).length(); i++)
   {
        if ((this->myData).at(i) == \'.\')
            numDots++;
    }
    return numDots;

}

/*overload << operator that returns the number of words and number of sentences*/
ostream& operator << (ostream& outs, const SpeechAnalyst &sa)
{
    if (sa.myData.length()>0)
        outs << \"Data has \" << sa.getNumberOfWords() << \" words and \" << sa.getNumberOfSentences() << \" sentences\";
    else
        outs << \"No Data to print Out\";

   return outs;
}

//empty the data
void SpeechAnalyst::clear()
{
    myData = \"\";
}

--------------------------------------------------------------------------------

/**
The C++ program SpeechAnalyst that tests the class SpeechAnalyst
and prints the corresponding methods results to console.
*/
//main.cpp
#include<iostream>
#include \"SpeechAnalyst.h\"
using namespace std;
int main()
{
   //Create an object of class SpeechAnalyst
   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.\" );
  
   //Add speech string to object sa
   sa.addStringData( speech );
   cout << sa << endl;

   //call clear method
   sa.clear();
   //create an arry of character pointer
   char * data = new char[ 50 ];
   //copy the string to pointer array,data
   strcpy( data, \"Muffin says Hello.\" );

   //data to object,sa
   sa.addData( data );
   cout << sa << endl;
   //data to object,sa
   sa.addData( data );
   cout << sa << endl;


   system(\"pause\");
   return 0;

}

--------------------------------------------------------------------------------

Sample Output:

No Data to print Out
Data has 29 words and 1 sentences
Data has 3 words and 1 sentences
Data has 5 words and 2 sentences

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 sentence
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 sentence
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 sentence
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 sentence

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site