在字符串中查找单词或短语的实例

时间:2018-12-06 05:30:18

标签: c++

我正在编写一个程序,用户在其中输入字符串,然后用户可以从菜单中进行选择,以执行诸如显示字符串中的单词数或字符串中的空格数之类的操作。除了可以找到特定单词或短语的实例数量的函数外,我所有的功能都在起作用。每当我输入一个单词或短语时,它就会说出现0次。这是我的全部代码,但是我只需要FindText函数的帮助。没关系,我是一个初学者。

#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;
}

1 个答案:

答案 0 :(得分:2)

  • string::find()返回size_type而不返回bool
  • 使用重载find(),该重载允许您指定起始位置。

        size_type find( const basic_string& str, size_type pos = 0 )
    
  • 找到字符串后,将其长度添加到起始位置,然后再次使用find查找下一个匹配项。

您可以这样修改功能:

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;
}