如何将字符添加到字符串中
How to add a char into a string
所以我正在创建一个刽子手游戏,并希望在字符串中添加一个字符。我想在gatherguess字符串中添加一个char of guess,直到gatherguest匹配hangman。请随意在我的代码中添加一些有用的提示,让我变得更好。如果你也能给我一些动态内存分配的示例代码,那就太好了。
#include <stdio.h>
#include <string.h>
#include <iostream> // std::cout
#include <stdio.h>
#include <string.h>
#include <iostream> // std::cout
#include <algorithm> // std::for_each
#include <vector>
#include <set>
#include <string>
bool isitdone(std::string foo, std::string hang){
return foo == hang ? true : false ;
}
int main(){
std::string hangman;
char guess;
std::string gatherguess; //Get the letters guessed.
std::cin >> hangman; //Player enter the word to guess.
bool checkstatement; // Check to see if game is over.
for(int i =0; i < 10; ++i) {
std::cin >> guess; //Individual characters to guess
std::string doo;
int wordsin;
doo = hangman;
int y;
if(doo.rfind(guess) != std::string::npos) {
std::cout << "Right " << guess << " Is in the word" << std::endl;
std::cout << std::endl;
checkstatement = isitdone(gatherguess,doo);// I want to add guess char into gatherguess
//then check to see if gatherguess is equal to the word then the game will be complete
if(checkstatement == true) {
return 0;
}
} else {
std::cout << "Wrong" << std::endl;
}
}
return 0;
}
首先,您应该用足够的占位符字符初始化gatherguess
:
auto hangman = std::string{};
std::cin >> hangman;
auto gatherguess = std::string{hangman.size(), '_'};
现在,您可以简单地覆盖"_"字符。
auto pos = hangman.find(guess)
if(pos != std::string::npos) {
// ...
do {
gatherguess[pos] = guess; // overwrite
pos = hangman.find(guess, pos); // find next ocurrence
} while(pos != std::string::npos)
// ...
}
我对您的代码进行了一些更改。它包含一些建议作为评论。
//#include <stdio.h> // Do not include a library if you don not use it, because it makes the performance worse.
//#include <string> // Do not include a library if you don not use it, because it makes the performance worse.
#include <iostream> // std::cout
//#include <stdio.h> // It is pointless to include a library twice.
//#include <string.h>
//#include <iostream> // std::cout
//#include <algorithm> // std::for_each
//#include <vector> // Do not include a library if you don not use it, because it makes the performance worse.
//#include <set> // Do not include a library if you don not use it, because it makes the performance worse.
#include <string>
bool isitdone(const std::string& foo, const std::string& hang){ // Passing argument by const reference makes performance much better.
return foo == hang ? true : false ; // Indenting makes the code much more readable.
}
int main(){
const int N=10; // Store constant number in constant variable, because you can change its' value later easily.
std::string hangman;
char guess;
std::string gatherguess; //Get the letters guessed.
std::cin >> hangman; //Player enter the word to guess.
bool checkstatement; // Check to see if game is over.
for(int i =0; i < N; ++i)
{
std::cin >> guess; //Individual characters to guess
std::string doo;
int wordsin;
doo = hangman;
int y;
if(doo.rfind(guess) != std::string::npos)
{
std::cout << "Right " << guess << " Is in the word" << std::endl;
std::cout << std::endl;
checkstatement = isitdone(gatherguess,doo);// I want to add guess char into gatherguess
//then check to see if gatherguess is equal to the word then the game will be complete
if(checkstatement == true)
{
return 0;
}
} else
{
std::cout << "Wrong" << std::endl;
}
}
return 0;
}
我认为你的程序有一个逻辑错误。如果一个单词包含超过10个不同的字符,会发生什么?如果小费是对的,不要计算。
您可以通过以下方式将字符添加到字符串中:
#include <iostream>
#include <string>
int main(){
std::string str="123";
str+='4';
std::cout<<str<<std::endl;
return 0;
}
相关文章:
- 如何将字符串添加到布谷鸟过滤器?
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 如何在C++中为字符串添加字符?
- 在特定位置为字符串添加空格
- 有没有办法将字符串添加到 Vector 中,但前提是它尚不存在?->C++
- 是什么使这个以'+'开头的字符串添加成为有效的语句?
- 如何使用函数将字符串添加到向量中
- 如何在 c++ 中将字符串添加到动态数组?
- 向字符串添加字符
- 向字符串添加位值
- 如何从用户那里接收多个字符串并将这些字符串添加到 c++ 中的数组中?
- 字符串添加数字 C++
- std::字符串添加到字符*
- C++ 向字符串添加"u"
- JSONCPP正在向字符串添加额外的双引号
- C 将N字符串添加到字符串流中
- 以简单且更少的代码方式将字符串添加在一起
- 字符串添加或减法操作员
- 将字符串添加到2D数组并显示COUT消息(没有错误消息?)的错误
- 如何使用递归向字符串添加逗号