I finished the majority of my program but having trouble wit

I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at the bottom. Thanks!

5.20 Program: Authoring assistant (C++)

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:


(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user\'s entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)

Ex:


(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)

Ex:


(4) Implement the GetNumOfWords() function. 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 except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)

Ex:


(5) Implement the FindText() function, 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 the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, callcin.ignore() to allow the user to input a new string. (3 pts)

Ex:


(6) Implement the ReplaceExclamation() function. 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 the PrintMenu() function, and then output the edited string. (3 pts)

Ex.


(7) Implement the ShortenSpace() function. 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. (3 pt)

Ex:

My Program:

#include
#include
#include
#include
using namespace std;

int GetNumOfNonWSCharacters(const string numChar) {

   int count = 0;

   for(double i = 0; i < numChar.length(); i++) {
       if(numChar[i] != \' \') {
           count++;
       }
   }

   return count;
}

int GetNumOfWords(const string numWords) {

   int count = 0;
   unsigned int i = 0;

   while(isspace(numWords.at(i))) {
       i++;
   }

   for(i = 0; i < numWords.length(); i++)
   {
       if(isspace(numWords.at(i))) {
           count++;

           while(isspace(numWords.at(i++))) {
               if(numWords.at(i) == \'\\0\') {
                   count--;
               }
           }
       }
   }

return count + 1;
}

string ReplaceExclamation (string& replaceChar) {

string newChar = replaceChar;

   for(unsigned int i = 0; i < replaceChar.size(); i++)
   {  
       if(replaceChar[i] == \'!\')
       {
           newChar[i] = \'.\';
       }
       replaceChar = newChar;
   }

return replaceChar;
}

string ShortenSpace (string& replaceSpace) {

   char *newSpace;
   int len = replaceSpace.size();
   newSpace = new char[len + 1];
   int i, k = 0;
  
   for(i = 0; i < len; k++)
   {
       newSpace[k] = replaceSpace[i];
      
       if (isspace(replaceSpace[i]))
       {
           while (isspace(replaceSpace[i])) {
               i++;
           }
       }
       else {
           i++;
       }
   }
  
   newSpace[k] = \'\\0\';
   replaceSpace = newSpace;
  
   return replaceSpace;
}

int FindText(string foundText, string usrPhrase) {

   int count = 0;  

   for(size_t offset = foundText.find(usrPhrase); offset != string::npos; offset = foundText.find(usrPhrase, offset + usrPhrase.size()))
   {
       ++count;
   }

return count;
}

void PrintMenu(string menuOptions) {

   cout << \"MENU\" << endl;
   cout << \"c - Number of non-whitespace characters\" << endl;
   cout << \"w - Number of words\" << endl;
   cout << \"f - Find text\" << endl;
   cout << \"r - Replace all !\'s\" << endl;
   cout << \"s - Shorten spaces\" << endl;
   cout << \"q - Quit\" << endl;
   cout << endl;

   return;
}

int main() {

   string usrStr;
   char usrOptions;
   string usrText;

   cout << \"Enter a sample text: \" << endl;
   getline (cin, usrStr);
   cout << \"You entered: \" << usrStr << endl;
   cout << endl;
  
   while (usrOptions != \'q\') {
  
PrintMenu(usrStr);
  
cout << \"Choose an option: \" << endl;
cin >> usrOptions;
  
       if (usrOptions == \'c\')
       {
           cout << \"Number of non-whitespace characters: \" << GetNumOfNonWSCharacters(usrStr) << endl;
           cout << endl;
       }
          
       else if (usrOptions == \'w\')
       {
           cout << \"Number of words: \" << GetNumOfWords(usrStr) << endl;
           cout << endl;
       }
          
       else if (usrOptions == \'f\')
       {
           cin.ignore();
           cout << \"Enter a word or phrase to be found: \" << endl;
           getline(cin, usrText);
           cout << \"\\\"\" << usrText << \"\\\" instances: \" << FindText (usrStr, usrText) << endl;
           cout << endl;
       }
      
       else if (usrOptions == \'r\')
       {
           cout << \"Edited text: \" << ReplaceExclamation(usrStr) << endl;
           cout << endl;
       }
          
       else if (usrOptions == \'s\')
       {
           cout << \"Edited text: \" << ShortenSpace(usrStr) << endl;
           cout << endl;
       }
}
  
   return 0;
}

Unit test 0/2 Tests that FindText() returns 5 for parameters \"more\" and \"We\'ll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!\" Test feedback FindText () incorrectly returns 0

Solution

Hi, Please find my implementation if findText method.

Please let me know in case of any issue:


int FindText(string foundText, string usrPhrase) {

int count = 0;
string::size_type start = 0;

while ((start = usrPhrase.find(foundText, start)) != string::npos) {
++count;
start += foundText.length(); // see the note
}

return count;
}

Explanation:
string::find takes a string to look for in the invoking object and a character position at which to start looking, and returns the position of the occurrence of the string, or string::npos if the string is not found.

The variable start starts at 0 (the first character) and in the condition of the loop, you use start to tell find where to start looking, then assign the return value of find to start. Increment the occurrence count; now that start holds the position of the string, you can skip usrPhrase.length() characters ahead and start looking again.

I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at th
I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at th
I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at th
I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at th
I finished the majority of my program, but having trouble with one key feature. My FindText() incorrectly returns 0 during a unit test. I included my code at th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site