C++ - 将"_"替换为字符

C++ - Replacing "_" with a character

本文关键字:字符 替换 C++      更新时间:2023-10-16

使用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变量中替换字符串,而是通过单词本身进行迭代,如果这有任何意义?

<</p>

使用第二个字符串,该字符串用下划线初始化。如果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;
    }
}