C++ 我不知道如何在字符串(句子)中找到单词(eg.banana,三明治)用户输入句子然后写出那个单词
C++ I don't know how to find a word(eg.banana,sandwich) inside of a string(sentence) THE USER ENTERS the sentence and then write that word out
我已经试过了,但老实说我被卡住了。
我试图找到第一个字符,然后搜索该子字符串的结尾(例如,如果单词是“sandwich”,它发现 's' 表明它是“sandwich”),然后写出三明治这个词。而且我是 C++ 的新手。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, word;
char a;
cout << "Enter the sentence that you desire: ";
getline(cin, s);
cout << "Enter the letter that you want: ";
cin >> a;
for (int i = 0; i < s.length; i++)
{
if (s[i] == a)
{
if (s[i] == '[=11=]')
{
word = s;
cout << word;
}
}
}
return 0;
}
你可以用std::find_if
找到一个词的结尾:
#include <algorithm>
#include <string>
template <typename Is>
std::string find_word(Is& stream, char needle) {
auto const nonword = [](char c) {
if ('a' <= c && c <= 'z') return false;
if ('A' <= c && c <= 'Z') return false;
if (c == '-') return false;
return true;
};
for (std::string w; stream >> w;) {
if (w.size() && w[0] == needle) {
auto const last = std::find_if(std::begin(w),std::end(w),nonword);
return std::string(std::begin(w),last);
}
}
return "";
}
这将任何流作为参数,包括 std::cin
,并且可以像这样调用:
std::cout << find_word(std::cin,'w') << "\n";
在流传递给您的每个块中专门找到最后一个字符很重要,因为默认情况下,流只会沿着空格剪切。所以如果你输入一句话:
Hello world!
您希望单词的结尾是 'd'
,而不是 '!'
。
请求有点模糊,但考虑到你发布的代码,我想我已经知道你打算做什么了。
最简单(但不一定是性能最好的)是使用字符串流,更准确地说是 istringstream。
你基本上用一个字符串(你从键盘传递的那个)构建它,然后你使用它就好像它是你的 cin(它充当规范化的 istream)。
此时您可以遍历句子的每个单词并检查第一个字母。
字符串的第一个字符是 myString[0] 或 myString.front()。这取决于你。
代码应如下所示:
#include <iostream> //cin/cout
#include <sstream> //istringstream
using namespace std ;
int main()
{
//first of all let's get our sentence AND the character you want
cout << "insert sentence here: " ;
string sentence ;
getline(cin, sentence) ;
cout << "insert the character here: " ;
char letter ;
cin >> letter ;
//then let's create an istringstream with said sentence
istringstream sentenceStream(sentence) ;
//let's then iterate over each word
string word ;
while(sentenceStream >> word)
{
//and see if the word starts with the letter we passed by keyboard
if(word.front() == letter)
{
cout << "the word \"" << word << "\" starts with '" << letter << "'\n" ;
}
}
return 0 ;
}
一些提示:
iostream 已经包含字符串,无需重新包含它。
[编辑](正如 whozcraig 所指出的,这不符合标准。守卫无论如何都会“否定”双重包含,所以是的,包含字符串不是错误。如评论中所述,我是尚未找到不包含字符串的 iostream 实现)[/Edit]
最好不要调用变量 's' 或 'a':使用名称
这使它易于识别。
我已经试过了,但老实说我被卡住了。
我试图找到第一个字符,然后搜索该子字符串的结尾(例如,如果单词是“sandwich”,它发现 's' 表明它是“sandwich”),然后写出三明治这个词。而且我是 C++ 的新手。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, word;
char a;
cout << "Enter the sentence that you desire: ";
getline(cin, s);
cout << "Enter the letter that you want: ";
cin >> a;
for (int i = 0; i < s.length; i++)
{
if (s[i] == a)
{
if (s[i] == '[=11=]')
{
word = s;
cout << word;
}
}
}
return 0;
}
你可以用std::find_if
找到一个词的结尾:
#include <algorithm>
#include <string>
template <typename Is>
std::string find_word(Is& stream, char needle) {
auto const nonword = [](char c) {
if ('a' <= c && c <= 'z') return false;
if ('A' <= c && c <= 'Z') return false;
if (c == '-') return false;
return true;
};
for (std::string w; stream >> w;) {
if (w.size() && w[0] == needle) {
auto const last = std::find_if(std::begin(w),std::end(w),nonword);
return std::string(std::begin(w),last);
}
}
return "";
}
这将任何流作为参数,包括 std::cin
,并且可以像这样调用:
std::cout << find_word(std::cin,'w') << "\n";
在流传递给您的每个块中专门找到最后一个字符很重要,因为默认情况下,流只会沿着空格剪切。所以如果你输入一句话:
Hello world!
您希望单词的结尾是 'd'
,而不是 '!'
。
请求有点模糊,但考虑到你发布的代码,我想我已经知道你打算做什么了。
最简单(但不一定是性能最好的)是使用字符串流,更准确地说是 istringstream。 你基本上用一个字符串(你从键盘传递的那个)构建它,然后你使用它就好像它是你的 cin(它充当规范化的 istream)。
此时您可以遍历句子的每个单词并检查第一个字母。 字符串的第一个字符是 myString[0] 或 myString.front()。这取决于你。
代码应如下所示:
#include <iostream> //cin/cout
#include <sstream> //istringstream
using namespace std ;
int main()
{
//first of all let's get our sentence AND the character you want
cout << "insert sentence here: " ;
string sentence ;
getline(cin, sentence) ;
cout << "insert the character here: " ;
char letter ;
cin >> letter ;
//then let's create an istringstream with said sentence
istringstream sentenceStream(sentence) ;
//let's then iterate over each word
string word ;
while(sentenceStream >> word)
{
//and see if the word starts with the letter we passed by keyboard
if(word.front() == letter)
{
cout << "the word \"" << word << "\" starts with '" << letter << "'\n" ;
}
}
return 0 ;
}
一些提示:
iostream 已经包含字符串,无需重新包含它。 [编辑](正如 whozcraig 所指出的,这不符合标准。守卫无论如何都会“否定”双重包含,所以是的,包含字符串不是错误。如评论中所述,我是尚未找到不包含字符串的 iostream 实现)[/Edit]
最好不要调用变量 's' 或 'a':使用名称 这使它易于识别。