如何解决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] != '\0'; ++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
方法来查找给定的字符(如果找不到,则返回字符的位置或 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
已猜出的字符,以确保同一字符只能猜出一次。
我将把它作为练习留给你来实施,因为它应该不会太难。如果您需要一些指示,请告诉我。
效率较低
第二种方式效率较低,但由于它是实时游戏,除非这些字符串非常长,否则不会产生任何影响。
只需保存一个字符串的副本并遍历它。每次检查您要检查的字符时,然后用正确的字符替换 _
单词中您所在的索引处的字符。您还可以保留一个标志来检查是否发生任何替换,如果没有,您就知道该字符不存在于单词中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。