AlexCPSC
AlexCPSC

Reputation: 51

Finding an instance of a word or phrase in a string

I'm writing a program where the user inputs a string and the user can then select from a menu to do things such as display the number of words in the string or number of spaces in the string. I have all functions working except for the one where I need to be able to find the number of instances of a specific word or phrase. Whenever I enter a word or phrase it says there are 0 occurrences. This is my entire code but again I just need help with my FindText function. Bare in mind I'm a beginner programmer.

#include <iostream>
#include <string>

using namespace std;
string user_text = " ";
string find_text = " ";
string replaced = " ";
char print_menu(char);
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
int FindText(string, string);
string ReplaceExclamation(string);
string ShortenSpace(string);
int main()
{

    char choice = ' ';

    cout << "Enter a sample text: ";
    getline(cin, user_text);

    choice = print_menu(choice);
    while (!(choice == 'q'))
    {
        switch (choice)
        {
        case 'c': //number of non-whitespace characters
            int not_space;
            not_space = GetNumOfNonWSCharacters(user_text);
            cout << "Number of non white space charactesr: " << not_space << endl;
            choice = print_menu(choice);
            break;
        case 'w': //number of words
            int words;
            words = GetNumOfWords(user_text);
            cout << "Number of words: " << words << endl;
            choice = print_menu(choice);
            break;
        case 'f': //find text
            int occurences;
            cout << "Enter a word or phrase to be found: ";
            cin.ignore();
            getline(cin, find_text);
            occurences = FindText(find_text, user_text);
            cout << find_text << " instances: " << occurences << endl;
            choice = print_menu(choice);
            break;
        case 'r': //replace all !'s
            replaced = ReplaceExclamation(user_text);
            cout << replaced << endl;
            choice = print_menu(choice);

            break;
        case 's': //shorten spaces
            replaced = ShortenSpace(user_text);
            cout << replaced << endl;
            choice == print_menu(choice);

            break;
        case 'q': //quit
            exit(0);
            break;
        default:
            cout << "Invalid choice please try again";
            choice = print_menu(choice);
        }
    }
    system("pause");
    return 0;
}
char print_menu(char choice)
{
    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 << "   Choose an option ";

    cin >> choice;
    return choice;
}
int GetNumOfNonWSCharacters(string text)
{
    int spaces = 0;
    int not_spaces = text.length();
    for (int i = 0; i < text.length(); i++)
    {
        if (isspace(text.at(i)) != false)
        {
            spaces += 1;
        }
    }
    not_spaces = not_spaces - spaces;
    return not_spaces;
}
int GetNumOfWords(string text)
{
    int words = 0;
    for (int i = 0; i < text.length(); i++)
    {
        if (text.at(i) == ' ')
        {
            words++;
        }
    }
    return words + 1;
}
int FindText(string find, string text)
{
    int count = 0;
    for (int i = 0; i < text.length(); i++)
    {
        if (text.find(find) == true)
        {
            count++;
        }

    }
    return count;
}
string ReplaceExclamation(string text)
{
    for (int i = 0; i < text.length(); i++)
    {
        if (text.at(i) == '!')
        {
            text.at(i) = '.';
        }
    }
    return text;
}
string ShortenSpace(string text)
{
    for (int i = 0; i < text.length(); i++)
    {
        if (text.at(i) == ' ' && text.at(i + 1) == ' ')
        {
            text.erase(text.begin() + i);
        }
    }
    return text;
}

Upvotes: 1

Views: 169

Answers (1)

P.W
P.W

Reputation: 26800

  • string::find() returns size_type and not bool
  • Use an overload of find() that allows you to specify the starting position.

        size_type find( const basic_string& str, size_type pos = 0 )
    
  • Once the string is found, add its length to the starting position and use find again to find the next occurrence.

You can modify your function thus:

int FindText(string find, string text)
{
    int count = 0;
    string::size_type start = 0;
    while ((start = text.find(find, start)) != string::npos) {
        ++count;
        start += find.length(); 
    }
    return count;
}

Upvotes: 2

Related Questions