C++ - 将"_"替换为字符
C++ - Replacing "_" with a character
使用C ,我正在尝试使Hangman游戏更好地使用C 和编程。无论如何,我面临的问题是,我不确定如何用用户猜测的字母替换字符串中的破折号。
我认为我的问题是从数组中随机选择了该单词的事实,我不确定如何在随机选择的字符串中找到由猜测的字符组成的位置。
。我已经评论了引起问题的区域。
#include <iostream>
#include <array>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <cstddef>
#include <algorithm>
using namespace std;
int main()
{
string words[3] = {"stack", "visual", "windows"};
string guess;
cout << "Welcome to hangman.n";
cout << "n";
srand(time(NULL));
int RandIndex = rand() % 3;
string selected = words[RandIndex];
for (int i = 1; i <= selected.size(); i++) {
cout << "_ ";
}
cout << "n";
cout << "nType in a letter: ";
cin >> guess;
cout << "n";
if (selected.find(guess) != string::npos) {
/*for (int i = 1; i <= selected.size(); i++) {
if (selected.find(guess) != string::npos) {
cout << "_ ";
} else {
cout << guess << " ";
}
}*/
} else {
cout << "nNay!n";
cout << "n";
}
cout << "n";
cout << "n";
system("PAUSE");
return 0;
}
我正在考虑使用replace()
函数,但是我在这里遇到的问题是,我不是在selected
变量中替换字符串,而是通过单词本身进行迭代,如果这有任何意义?
使用第二个字符串,该字符串用下划线初始化。如果find
函数不返回返回string::npos
,它将返回字符串中的位置,这是您在字符串中也应使用下划线更改的位置。
您实际上需要使用第二个字符串来存储"猜测"字符串;这是因为您需要跟踪所有猜测的字母并显示它们。
类似:
string s ="test";
string t=""; //empty string
for(int i=0;i<s.size();i++)
t.append("_"); //initialize the guess string
cout<<t<<'n';
char c;
cin >> c;
int pos = s.find(c); //get the first occurrence of the entered char
while(pos!=-1) //look for all occurrences and replaced them in the guess string
{
t.replace(pos,1,1,c);
pos = s.find(c, pos+1);
}
我认为您需要在循环时保持一些额外的状态 - 以跟踪哪些字母尚未猜测。
您可以添加一个新的字符串Current_State,最初设置为与单词相同的长度,但所有下划线。然后,当玩家猜测一封信时,您会在原始单词中找到该字母的所有实例,并用猜测的字母替换了下划线,但在Current_state中发现的所有位置。
首先,我将初始化一个新字符串以显示隐藏的字:
string stringToDisplay = string( selected.length(), '_');
然后,对于用户给我的每个字母,我会这样循环:(假设猜测是字母)
size_t searchInitPos = 0;
size_t found = selected.find(guess, searchInitPos));
if (found == string::npos)
{
cout << "nNay!n";
cout << "n";
}
while( found != string::npos)
{
stringToDisplay[found] = guess;
searchInitPos = found+1;
found = selected.find(guess, searchInitPos));
}
cout << stringToDisplay;
希望这会有所帮助
我认为应该是:
string words[3] = {"stack", "visual", "windows"};
char guess;
string display;
cout << "Welcome to hangman.n";
cout << "n";
srand(time(NULL));
int RandIndex = rand() % 3;
string selected = words[RandIndex];
for (int i = 0; i < selected.size(); i++) {
display.insert(0, "_ ");
}
cout << display;
while(display.find("_ ") != string::npos) {
cout << "n";
cout << "nType in a letter: ";
cin >> guess;
cout << "n";
bool flag = false;
for (int i = 0; i < selected.size(); i++) {
if (selected[i] == guess) {
display.replace(i*2, 1, 1, guess);
flag = true;
}
}
if (!flag) {
cout << "nNay!n";
cout << "n";
} else {
cout << display;
}
}
相关文章:
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 我想仅使用递归将字符"a"替换为字符"z"
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 如何将像'A'这样的单个字符替换为类似"10"?
- Qt:文件名中的非 ASCII 字符替换为"?"
- 我无法用字符替换重复的数字"a"
- QtLineEdit 占位符:如何让用户在插入模式下将字符 1 替换为 1
- 将一个特定字符替换为正则表达式匹配中的另一个字符
- 如何将所有出现的字符替换为给定的字符串
- 将一个数组到另一个数组的字符替换为字符串
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 将字符串中的所有非 ASCII 字符替换为其等效的 ASCII
- 将utf8编码的字符串转换为本地8位编码的字符串,并将无法确定的字符替换为空白
- C++-用非ascii字符替换文件中的特殊字符
- 将字符串中的 č,š,ć 等字符替换为其 html 代码C++
- 用连字符替换字符串中的文本
- 用不同字符替换单词的代码
- 将空白字符替换为 %20 的字符串,忽略所有尾随空格
- 将部分字符*替换为较小的字符*
- 将字符串中的字符替换为某个字符串