3 SEPARATE FILES HEADER CPP AND DRIVER The purpose of this a

3 SEPARATE FILES

HEADER

CPP

AND DRIVER

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 \"

Solution

//Feb_13_string.h

#include<iostream>
#include<string>
using namespace std;

class SpeechAnalyst
{
   string myData;
public:
   SpeechAnalyst();
   void clear();
   void addData(char *stuff);
   void addStringData(string stuff);
   int getNumberOfWords() const;
   int getNumberOfSentences() const;

   friend ostream& operator<<(ostream& out, const SpeechAnalyst& sa);
};

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

//Feb_13_string.cpp

#include\"Feb_13_string.h\"

SpeechAnalyst::SpeechAnalyst()
{
   myData = \"\";
}

void SpeechAnalyst::clear()
{
   myData = \"\";
}

void SpeechAnalyst::addData(char *stuff)
{
  
   if (myData.length() ==0)
   {
       myData = stuff;
   }
   else
   {
       myData += \' \';
       myData += stuff;
   }
}

void SpeechAnalyst::addStringData(string stuff)
{
   if (myData.length() == 0)
   {
       myData = stuff;
   }
   else
   {
       myData += \' \';
       myData += stuff;
   }
}

int SpeechAnalyst::getNumberOfWords() const
{
   int count = 0,n;
   for ( n = 0; n < myData.length(); n++)
   {

       if (myData[n] == \' \' )
       {
           count = count + 1;
       }
      
   }
   if (myData[n] == \'\\0\' )
       ++count;
   return count;
}
int SpeechAnalyst::getNumberOfSentences() const
{
   int count = 0;
   for (int i = 0; i < myData.length(); i++)
   {
       if (myData[i] != \'.\')
           continue;
       else
           count++;
   }
   return count;
}

ostream& operator<<(ostream& out, const SpeechAnalyst& sa)
{
   if (sa.myData == \"\")
       cout << \"No data to display\";
   else
   {
       out << sa.myData << endl;
       out << \"Data has \" << sa.getNumberOfWords() << \" words\" << \" and \" << sa.getNumberOfSentences() << \" sentences\" << endl;;
       return out;
   }

}

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

//Feb_13_string_Main.cpp

#include\"Feb_13_string.h\"

int main()
{
   SpeechAnalyst sa;
   cout << sa << endl;
   string speech(\"Four score and seven years ago our fathers brought forth on this continet a new nation,conceived in liberty and dedicated to the proposition that all men are created equal.\");
   //string speech(\"Four score and seven years ago our fathers brought forth on this continet a new nation.\");
   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;
}

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

//output

No data to display
Four score and seven years ago our fathers brought forth on this continet a new
nation,conceived in liberty and dedicated to the proposition that all men are cr
eated equal.
Data has 29 words and 1 sentence

Muffin says Hello.
Data has 3 words and 1 sentence

Muffin says Hello. Muffin says Hello.
Data has 6 words and 2 sentences

3 SEPARATE FILES HEADER CPP AND DRIVER The purpose of this assignment is to practice using strings and C-strings. Your task is to create a class that counts wor
3 SEPARATE FILES HEADER CPP AND DRIVER The purpose of this assignment is to practice using strings and C-strings. Your task is to create a class that counts wor
3 SEPARATE FILES HEADER CPP AND DRIVER The purpose of this assignment is to practice using strings and C-strings. Your task is to create a class that counts wor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site