gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:19:30 24 4
gpt4 key购买 nike

我正在编写一个程序,用户在其中输入一个字符串,然后用户可以从菜单中进行选择以执行诸如显示字符串中的单词数或字符串中的空格数之类的操作。除了需要能够找到特定单词或短语的实例数的功能外,我的所有功能都在工作。每当我输入一个单词或短语时,它都会说有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com