如何判断一个字符是否在字符串中 C++

how to tell if a character is inside a string c++

我正在自己制作一个刽子手游戏,我 运行 遇到了一个问题,我需要知道猜测是否在秘密词中。如果是,我会将 bool innum 更改为 true,如果不是,它将保持为 false。我查了一下,找不到任何有用的东西。另外,名称 printe 只是字符串的名称,它正是我所命名的。 这是我正在使用的代码:

using namespace std;
#include <iostream>
#include <conio.h>


void title();

void rightanswer();

void try1();
void try2();
void try3();
void try4();
void try5();
void try6();

void spacer();

int main()
{
    bool innum = false;
    int printe = 0, attempts_wrong = 0, trynum = 6;
    char guess;
    string secretword, hint1, hint2;
    title();
    // the welcoming senteces
    cout << "Welcome to HANG MAN\n\n" << endl;
    cout << "Please enter the secret word (no spaces): ";
    cin >> secretword;

    // the hints
    cout << "\nenter the first hint: ";
    cin >> hint1;


    cout << "\nenter the second hint: ";
    cin >> hint2;

    //explanation for hints
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; //so guesser cant see word

    cout << "\nthe hints will be used as the guesser runs out of attemptts" << endl;
    cout << "the first hint will be used immediately\n\n" << endl;
    cout << "press any button to start...";
    _getch();

    cout << "\n\n" << endl;

    for (int i = 0; secretword[i] != '[=10=]'; ++i)
    {
        printe++;
    }

    rightanswer();
    cout << "\nyour word is " << printe << " letters long" << endl;

    if (attempts_wrong == 0)
    {
        cout << "your first hint is: ";
        cout << hint1 << endl;
    }
    if (attempts_wrong == 3)
    {
        cout << "your second hint is: ";
        cout << hint2 << endl;
    }


   

    cout << "enter a letter: ";
    cin >> guess;

    // im gonna have the code go here
    // <-----------------------------


    if (innum == true)
    {
        spacer();
        cout << guess << " is in the secret word" << endl;
        rightanswer();
    }
    else if (innum == false)
    {
        spacer();
        cout << guess << " is not in the secret word" << endl;
        rightanswer();
        attempts_wrong++;
    }

    return 0;
}



void title() {
    
    cout << "*****************************************" << endl;
    cout << "*                           _____       *" << endl;
    cout << "*   |     |    /\   |\   | |            *" << endl;
    cout << "*   |_____|   /__\  | \  | |  ___       *" << endl;
    cout << "*   |     |  /    \ |  \ | |     |      *" << endl;
    cout << "*   |     | /      \|   \| |_____|      *" << endl;
    cout << "*                                       *" << endl;
    cout << "*      |\     /|    /\   |\   |         *" << endl;
    cout << "*      | \   / |   /__\  | \  |         *" << endl;
    cout << "*      |  \ /  |  /    \ |  \ |         *" << endl;
    cout << "*      |   V   | /      \|   \|         *" << endl;
    cout << "*                                       *" << endl;
    cout << "*****************************************" << endl;

}


//head, body, 2 arms, 2 legs - 6 in total

void rightanswer() {
    //if the guess is right and the start

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "       |   " << endl;
    cout << "     __|__ " << endl;
}

void try1() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "       |   " << endl;
    cout << "     __|__ " << endl;
}
void try2() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "   |   |   " << endl;
    cout << "     __|__ " << endl;

}
void try3() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|   |   " << endl;
    cout << "     __|__ " << endl;

}
void try4() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "     __|__ " << endl;

}
void try5() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "  /  __|__ " << endl;

}
void try6() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "  / \__|__ " << endl;

    cout << " YOU LOSE" << endl;
    cout << "you have run out of guesses";
    exit(0);

}

// it creates a line to differ one try from the other
void spacer() {
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}

我不知道要展示多少,所以就这些了,它可能不是最好的,但它确实有效。我知道我在底部没有一些空隙功能,但它现在可以工作。如果您有任何建议,请告诉我。也别太苛刻了,我是初学者

您可以使用std::string::find方法找到给定的字符(returns字符的位置或std::string::npos如果没有找到):http://www.cplusplus.com/reference/string/string/find/

示例:

std::string str = "Hello.World";

auto found = str.find('.');
if (found != std::string::npos)
    std::cout << "Period found at: " << found << std::endl;

在您的情况下,它可能看起来像这样:

innum = (secretword.find(guess) != std::string::npos);

尝试string.find

auto i =secretword.find(guess); 给你索引。如果等于 std::string::npos,则不存在。

此外,std::string 有一个 size() 方法,您可以用它来放置字母计数。

有几种方法可以解决这个问题。如果您真的想简单地查找字符串是否包含字符,则可以使用以下方法:

char needle;
haystack.find(needle) != string::npos;

正如其他人正确回答的那样。但是,在这种情况下,我认为您正在寻找稍微不同的东西。这是一个刽子手游戏,所以您实际上可能想要做的是显示个人实际猜对的单词部分。

高效

如果我们假设:display 是一个字符串,用 _(或某个不是可猜单词中有效字母的字符)作为字符串长度的每个字符初始化,您可以使用它来部分显示它。

如果我们有一个 map<char, vector<size_t>> 准确地告诉我们每个字符出现的索引,这很容易计算,我们可以进行猜测并遍历相应的 vector 索引和用实际字符替换 display 中的字符,然后从 map 中删除该字符。一旦 map 为空,则整个字符串都被猜到了。您可能还想维护一个 set 个已经被猜过的字符,以确保同一个字符只能被猜到一次。

我会把它作为练习留给你来实现,因为它应该不会太难。如果您需要一些指示,请告诉我。

效率较低

第二种方法效率较低,但由于它是实时游戏,除非这些字符串真的很长,否则不会有什么不同。

只需保存字符串的副本并遍历它。每次检查要检查的字符,然后用正确的字符替换 _ 单词中索引处的字符。您还可以保留一个标志,检查是否有任何替换发生,如果没有,您就知道该字符不存在于单词中。