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

时间:2015-02-12 13:39:33

标签: c++

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

我尝试删除,删除和替换功能,但它并不适合我。因为我在const char *中有一个符号,我无法将其转换为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;
}

替换功能到目前为止还没有工作。

1 个答案:

答案 0 :(得分:0)

您无法替换字符串中的字符。你应该做的是使用std :: string作为std :: vector&lt; char&gt; (因为它非常相似)。

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

  

word [2] = yourLetter;

你甚至不需要知道之前在那个地方的东西,所以你不需要更换它 - 分配更容易。