Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please do this in C++ and use good format. Also some explanatations for some con

ID: 3736014 • Letter: P

Question

Please do this in C++ and use good format. Also some explanatations for some confusing code would be appreciated.

(3) Implement the function getNumOfNonWSCharacters(). getNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. The function isspace(c) will be useful here. Use the following prototype: int getNumOfNonWSCharacters(const string usrStr)

4) Implement the function getNumOfWords(). getNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached, so if you see a space, and the character before it is not a space, then add 1 to your word count. The last word in the paragraph will likely not have a space after it, so just add an extra word to your total if the last character in the paragraph is not a space. Use the following prototype: int getNumOfWords(const string usrStr)

(5) Implement the function findText(), which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In main, if the user selects option f, prompt the user for a word or phrase to be found and then call findText(). Make sure the order of arguments in your call matches the order of the parameters. Also be sure there are no characters left in the read buffer before you try to read the user's search string. If there are, get rid of them with cin.ignore(). Use the following prototype: int findText(const string toFind, string usrStr)

(6) Implement the function replaceExclamation(). replaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. replaceExclamation() DOES NOT output the string. Call replaceExclamation() in main, and then output the edited string. Use the following prototype: void replaceExclamation(string& usrStr)

(7) Implement the function shortenSpace(). shortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. Use the following prototype: void shortenSpace(string& usrStr).

Use the following for (7):

for (i = 0; i < usrStr.size() - 1; i++)
   {
      if(isspace(usrStr.at(i)) && isspace(usrStr.at(i+1)))
      {
         usrStr.erase(i,1);
         i--;
      }
   }

Explanation / Answer

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

//problem 3
int getNumOfNonWSCharacters(const string usrStr)
{
int res = 0;
for(int i = 0 ; usrStr[i] != ''; i++)
{
if(!isspace(usrStr[i]))
{
res++;
}
}
  
return res;
}

//problem 4
int getNumOfWords(const string usrStr)
{
int wordCount = 0;
for(int i = 1 ; usrStr[i] != ''; i++)
{
if(isspace(usrStr[i-1]) && usrStr[i-1] != usrStr[0])
{
wordCount++;
}
}
wordCount++;
  
return wordCount;
}

// problem 5
int findText(const string toFind, string usrStr)
{
int numWords = 0;
stringstream ss(usrStr);
string word;

while (ss >> word) {
if (word == toFind) {
numWords++;
}
}
return numWords;
  
}

//problem 6
void replaceExclamation(string& usrStr)
{
for(int i = 0 ; usrStr[i] != ''; i++)
{
if(usrStr[i] == '!')
{
usrStr[i] = '.';
}
}
}
  
//problem 7
void shortenSpace(string& usrStr)
{
for (int i = 0; i < usrStr.size() - 1; i++)
{
if(isspace(usrStr.at(i)) && isspace(usrStr.at(i+1)))
{
usrStr.erase(i,1);
i--;
}
}
}

int main()
{
string enteredString;
cout<<"enter the string"<<endl;
getline(cin,enteredString);
  
while(true)
{
char choice;   
cout<< "Enter the choice"<<endl;
cout<< "W...for get Num Of Non WS Characters"<<endl;
cout<< "P... For get Num Of Words"<<endl;
cout<< "F...for findText"<<endl;
cout<< "R... For replace Exclamation"<<endl;
cout<< "S... For shorten Spaceand"<<endl;
cout<< "Q... Quit"<<endl;
cin>>choice;

if(choice == 'F')
{
cout << "Enter a word or pharse to be found:" << endl;
cin.ignore();
string wordPhrase;
getline(cin, wordPhrase);
int findTextRes = findText(wordPhrase, enteredString);
cout << "instances a word or phrase: = " <<findTextRes<<endl;
}
else if(choice == 'W')
{
int numOfNonWSCharacters = getNumOfNonWSCharacters(enteredString);
cout<<"Num Of Non WS Characters = "<<numOfNonWSCharacters<<endl;
}
else if(choice == 'P')
{
int numOfWords = getNumOfWords(enteredString);
cout<<"Num Of words = "<<numOfWords<<endl;
}
else if(choice == 'R')
{
replaceExclamation(enteredString);
cout << "Replaced string : = " <<enteredString<<endl;
}
else if(choice == 'S')
{
shortenSpace(enteredString);
cout << "Shorten Space string : = " <<enteredString<<endl;
}
else if(choice == 'Q')
{
break;
}

}
return 0;
}

//PLEASE PROVIDE FEEDBACK THUMBS UP