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

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

最佳答案

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

关于c++ - 在字符串中查找单词或短语的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53645179/

相关文章:

c++ - 删除 opencv 和 c++ 中的边界线

c++ - 在 C++ 中使用带 vector 的智能指针时崩溃

c++ - openCV 的 triangulatePoints() 的结果应该是什么样的?

c++ - std::vector::erase 无效范围

c++ - C++ 字符串中的字符串游标/标记

c++ - 如何使用 STL (C++) 避免重新分配

c++ - 测试是否使用 Boost.Log 生成日志消息

c++ - 你能解释一下下面的 C/C++ 语句吗?

c++ - Pointer-to-Pointer 在 Pointer 不存在的情况下崩溃

c++ - 被通告扼杀的 friend 功能包括