如何在字符串C++中添加任何符号而不是另一个符号

How to add any symbol instead another symbol in string C++?

本文关键字:符号 另一个 任何 添加 字符串 C++      更新时间:2023-10-16

我正在编写一个"Hangman"游戏机应用程序,我想在字符串中添加一个符号,而不是另一个符号。

我尝试过擦除、移除和替换函数,但对我来说不起作用。因为我在constchar*中有一个符号,无法将其转换为char。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
string conceivedword;
string hint;
string choice;
string first_player_word;
string characterstring;
int counter = 0;
void first_player() {
    cout << "Welcome to Hangman! //Firstgamerzone " << endl;
    cout << "Please conceive the word" << endl;
    getline(cin, conceivedword);
    cout << "Type the hint" << endl;
    getline(cin, hint);
    system("cls");
    cout << " The hint is: " << hint << endl;
}
void second_player() {
    while (0 == 0) {
        cout << "Are you inputting word or character?" << endl;
        //while (0 == 0) {
        getline(cin, choice);
        if (choice == "word" || choice == "sitkva" || choice == "sityva") {
            cin >> first_player_word;
            if (first_player_word == conceivedword) {
                cout << "Congrats! You're winner! shen moige gazqura :D " << endl;
                break;
            }
            else {
                counter += 1;
                if (counter > 5) {
                    cout << "Bad luck. You lost. You had only 5 attempts." << endl;
                    break;
                }
                cout << "Bad luck. Try again" << endl;
                //second_player();
            }
        }
        else if (choice == "character" || choice == "aso") {
            cin >> characterstring;
            if (characterstring.length() > 1) cout << "Hey,Your Lier! You lost." << endl;
            else {
                counter += 1;
                const char *character = characterstring.c_str();
                int exists = conceivedword.find(character);
                //cout << exists << endl;
                int index = conceivedword.find(character);
                if (exists > 0) {
                    if (counter > 5) {
                        cout << "You lost. You had only 5 attempts" << endl;
                        break;
                    }
                    cout << "Right. You rock: " << endl;
                    int length = conceivedword.length();
                    for (int i = 0; i < conceivedword.length(); i++) {
                        if (i == index) {
                            cout << character;
                            //replace(conceivedword.begin(), conceivedword.end(), '_', character);
                        }
                            //cout << conceivedword.length() << endl;
                        //if (i == index) {
                            //replace(conceivedword.begin(), conceivedword.end(), '_', '*');
                            //}
                        cout << "_" << " ";
                        //cout << endl;
                    }
                    second_player();
                    cout << endl;
                }
                if(exists <= 0) {
                    cout << "Not right. Try again" << endl;
                    second_player();
                }
            }
            break;
        }
    }
}
int main() {
    system("Color 3");
    first_player();
    second_player();
    system("pause");
    return 0;
}

到目前为止,替换功能不起作用。

不能替换字符串中的字符。您应该使用std::string作为std::vector<char>(因为它非常相似)。

你可以将字母分配到单词中的某个位置:

word[2]=你的信;

你甚至不需要知道以前在那个地方是什么,所以你不需要替换它——分配更容易。

相关文章: