如何在c++中检查一个字符是否在字符串中
How to check if a char is in a string in C++?
好吧,我觉得这应该是非常简单的,但我怎么也想不出来。我试图写一个函数来找到一个字符在一个绞刑程序的字符串。所以我想到了:
int drawHangman(char trY, string wordA)
{
int wrongTries = 0,
if(wordA.find(trY) != string::npos) //checking to see if char is in word
wrongTries++; //if its not, ++
else
wrongTries = wrongTries; //if it is, do wrong tries remains the same
return wrongTries; //return # wrong tries
}
Suggetions吗?
您已经找到了如何检查字符是否在给定字符串内(find
函数)。保持简单:
bool isInside(const std::string & str, char c)
{
return str.find(c) != std::string::npos;
}
试着把每个任务分成一个函数,每个函数只执行一件简单的事情。查找一个字符是否在给定字符串中的函数应该只做这些。计算这个函数返回false
的次数是另一个函数的事情,因为这是一个不相关的任务(也就是说,与搜索无关)。
你的drawHangman
函数应该只关注实际绘制刽子手,通过参数给出用户失败的次数,例如
如果我正确理解了您的意思,那么您需要的是以下内容
size_t drawHangman( const std::string &word, char c )
{
static size_t wrongTries = 0;
if ( word.find( c ) != std::string::npos )
{
return ( wrongTries = 0 );
}
else
{
return ( ++wrongTries );
}
}
如果你需要重置wrongTries,你可以用一些任意的值来调用它,例如
drawHangman( " ", ' ' );
如果你不想像我上面展示的那样调用函数来重置wrongTries,那么你可以这样定义它
size_t drawHangman( const std::string &word = " ", char c = ' ' )
{
static size_t wrongTries = 0;
if ( word.find( c ) != std::string::npos )
{
return ( wrongTries = 0 );
}
else
{
return ( ++wrongTries );
}
}
你可以把它简单地称为
drawHangman();
复位wrongTries.
如果您需要不区分大小写的搜索,那么您可以使用标准算法std::find_if
。例如
#include <algorithm>
size_t drawHangman( const std::string &word, char c )
{
static size_t wrongTries = 0;
suto it = std::find_if( word.begin(), word.end(),
[=]( char t ) { return ( std::toupper( t ) == std::toupper( c ) ); } );
if ( it != word.end() )
{
return ( wrongTries = 0 );
}
else
{
return ( ++wrongTries );
}
}
另一方面,如果您需要计算字符串中不存在的字符的所有情况,则可以按以下方式编写函数
int drawHangman( const std::string &word = "", char c = ' ' )
{
static int wrongTries = 0;
if ( word.empty() )
{
wrongTries = 0;
return wrongTries;
}
if ( word.find( c ) != std::string::npos )
{
return ( -wrongTries );
}
else
{
return ( ++wrongTries );
}
}
所以你可以用下面的方法检查字符串中是否存在一个字符
if ( drawHangman( SomeString, SomeChar ) <= 0 )
{
// the character is found in the string
}
else
{
// the character is not found in the string
}
如果你需要重置wrongTries,你可以调用这个函数
drawHangman();
这将是大小写敏感的,所以如果wordA =" space" (我将假设这个词已经是小写的),你寻找一个'C'你不会找到一个'C'寻找一个'C'会找到一个'C'虽然。
您可以使用std::tolower来确保trY字符是小写的,同样假设word已经是小写的。*
bool IsValidLetter(char trY, const std::string& wordA)
{
if (wordA.find(std::tolower(trY)) != std::string::npos)
{
return true;
}
return false;
}
注意,在这种情况下,我删除了wrongTries计数器,因为您只是在测试(此时)该字母是否存在于单词中。
那么你可能想要保存一个容器,可能是一个向量,包含到目前为止所有尝试过的字母,这可能是有用的伪代码。
int CountWrongLetters(attemptedLetters, wordA)
{
int wrong = 0;
foreach letter in attemptedLetters
{
if (IsValidLetter(letter, wordA) == false)
{
wrong++;
}
}
return wrong;
}
其他地方将处理逻辑,根据返回的数字绘制hangman显示的每个部分,以及结束游戏的逻辑。
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用动态分配将 char* 复制到另一个字符**
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 使用连续分隔符和空最后一个字符进行拆分
- 如何比较文件中包含的下一个字符
- 如何检查字符串中的最后一个字符是否是某个字符并将其从字符串中删除?(C++)
- 如何从文本文件中一次读取一个字符
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 读取输入后,输出丢失了一个字符
- 比较最后一个字符,不区分大小写(带谓词?
- 如果一个字符,比如说"9",我减去"0",我在结果上使用 is 数字,我是真还是假
- 为什么 std::istream::gcount 返回的字符比预期的多一个字符
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- 使用 char 将一个字符数组分配给另一个字符数组时出现奇怪的行为
- 在弦乐器中删除最后一个字符