如何在c++中检查一个字符是否在字符串中

How to check if a char is in a string in C++?

本文关键字:一个 字符 是否 字符串 c++ 检查      更新时间:2023-10-16

好吧,我觉得这应该是非常简单的,但我怎么也想不出来。我试图写一个函数来找到一个字符在一个绞刑程序的字符串。所以我想到了:

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显示的每个部分,以及结束游戏的逻辑。

相关文章: