如何在字符串C++中添加任何符号而不是另一个符号
How to add any symbol instead another symbol in string C++?
我正在编写一个"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]=你的信;
你甚至不需要知道以前在那个地方是什么,所以你不需要替换它——分配更容易。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- 当我尝试将文件添加到另一个文件时出现怪异符号时
- 如何检查一个名称是否与另一个名称引用相同的符号
- 未解决的外部符号,带有静态lib,它使用了另一个静态lib
- 未解析的外部符号来自一个项目中的静态库,而不是另一个项目.(C++)
- 另一个与未解决符号相关的问题
- 如何在字符串C++中添加任何符号而不是另一个符号
- 另一个:建筑x86_64:的未定义符号
- [C++]将一个字符串切成字符串,由另一个字符串中给出的符号以外的任何内容分隔
- 无法调用另一个类-ERR-未解析的外部符号
- 另一个错误LNK2019:函数___tmainCRTStartup中引用未解析的外部符号_WinMain@16
- 用与另一个库相同的符号构建一个库
- 基于另一个已定义符号的条件#define
- 有没有办法从 ELF 中提取符号,并使用 LD 将另一个文件与上一个 ELF 中定义的符号链接在一起
- LNK2001:尝试从另一个 DLL 调用对象的方法时,未解析的外部符号
- 无法将一个项目的头和 CPP 文件与另一个项目链接 LNK2019:未解决的外部符号错误