简体   繁体   English

如何将字符添加到字符串中

[英]How to add a char into a string

So I am creating an hangman game and want to add a char into a string.所以我正在创建一个刽子手游戏并想在字符串中添加一个字符。 I want to add a char of guess to the gatherguess string until the gatherguess matches hangman.我想在gatherguess 字符串中添加一个猜测字符,直到gatherguess 匹配刽子手。 Feel free to add some helpful tips to my code that will make me better.随意在我的代码中添加一些有用的提示,这将使我变得更好。 Also if it would be more then nice if you can also give me some sample code with dynamic memory allocation.此外,如果您还可以给我一些带有动态内存分配的示例代码,那就更好了。

#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;
}

First of all, you should initialize gatherguess with enough placeholder characters:首先,您应该使用足够的占位符字符初始化gatherguess

auto hangman = std::string{};
std::cin >> hangman;

auto gatherguess = std::string{hangman.size(), '_'};

Now you can simply overwrite the '_' characters.现在您可以简单地覆盖“_”字符。

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)

    // ...
}

I made some changes on your code.我对您的代码进行了一些更改。 It contains some pieces of advice as comment.它包含一些建议作为评论。

//#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;
}

I think there is a logical mistake in your program.我认为您的程序存在逻辑错误。 What happens if a word contains more than 10 different characters?如果一个单词包含超过 10 个不同的字符会怎样? Do not count if the tipp is right.如果tipp 是对的,不要计算。

You can add a char to a string this way:您可以通过以下方式向字符串添加字符:

#include <iostream>
#include <string>

int main(){
    std::string str="123";
    str+='4';
    std::cout<<str<<std::endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM