Is it possible to add to char type variable to get string ?












-1














Let's say I have 2 string variables



string str1 = "A";
string str2 = "B";
string str3;


Doing



str3 = str1 + str2;


Would get me



str3 = "AB"


My question is would it be possible to do addition in a somewhat similar fashion if instead of string type, str1 and str2, there would be char type values? If not, could anyone suggest me some smart workarounds if possible?



char str1 = 'A';
char str2 = 'B';
string str3; // <------ I need to get "AB" in str3 somehow


Thank you










share|improve this question
























  • See std::string::push_back
    – Ayxan
    Nov 19 '18 at 15:24










  • @Jokubas11 .. see more ways given below..
    – code_cody97
    Nov 22 '18 at 9:36


















-1














Let's say I have 2 string variables



string str1 = "A";
string str2 = "B";
string str3;


Doing



str3 = str1 + str2;


Would get me



str3 = "AB"


My question is would it be possible to do addition in a somewhat similar fashion if instead of string type, str1 and str2, there would be char type values? If not, could anyone suggest me some smart workarounds if possible?



char str1 = 'A';
char str2 = 'B';
string str3; // <------ I need to get "AB" in str3 somehow


Thank you










share|improve this question
























  • See std::string::push_back
    – Ayxan
    Nov 19 '18 at 15:24










  • @Jokubas11 .. see more ways given below..
    – code_cody97
    Nov 22 '18 at 9:36
















-1












-1








-1







Let's say I have 2 string variables



string str1 = "A";
string str2 = "B";
string str3;


Doing



str3 = str1 + str2;


Would get me



str3 = "AB"


My question is would it be possible to do addition in a somewhat similar fashion if instead of string type, str1 and str2, there would be char type values? If not, could anyone suggest me some smart workarounds if possible?



char str1 = 'A';
char str2 = 'B';
string str3; // <------ I need to get "AB" in str3 somehow


Thank you










share|improve this question















Let's say I have 2 string variables



string str1 = "A";
string str2 = "B";
string str3;


Doing



str3 = str1 + str2;


Would get me



str3 = "AB"


My question is would it be possible to do addition in a somewhat similar fashion if instead of string type, str1 and str2, there would be char type values? If not, could anyone suggest me some smart workarounds if possible?



char str1 = 'A';
char str2 = 'B';
string str3; // <------ I need to get "AB" in str3 somehow


Thank you







c++ string char






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:24









eyllanesc

74.2k103156




74.2k103156










asked Nov 19 '18 at 15:12









Jokubas11

1




1












  • See std::string::push_back
    – Ayxan
    Nov 19 '18 at 15:24










  • @Jokubas11 .. see more ways given below..
    – code_cody97
    Nov 22 '18 at 9:36




















  • See std::string::push_back
    – Ayxan
    Nov 19 '18 at 15:24










  • @Jokubas11 .. see more ways given below..
    – code_cody97
    Nov 22 '18 at 9:36


















See std::string::push_back
– Ayxan
Nov 19 '18 at 15:24




See std::string::push_back
– Ayxan
Nov 19 '18 at 15:24












@Jokubas11 .. see more ways given below..
– code_cody97
Nov 22 '18 at 9:36






@Jokubas11 .. see more ways given below..
– code_cody97
Nov 22 '18 at 9:36














4 Answers
4






active

oldest

votes


















4














Sure. There are several ways. Heres a few.



#include<string>
#include<iostream>

using namespace std;

int main()
{
char str1 = 'A';
char str2 = 'B';
string str3;
//using operator+=
str3 += str1;
str3 += str2;

cout << str3 << endl;
str3.clear();
//using push_back
str3.push_back(str1);
str3.push_back(str2);

cout << str3 << endl;
str3.clear();
//using array access
str3.resize(2);
str3[0] = str1;
str3[1] = str2;
cout << str3 << endl;

return 0;
}





share|improve this answer































    3














    Yes, it's entirely possible to concatenate char to a std::string, you just need one of the operands to be a std::string, otherwise you are essentially adding integers.



    #include <iostream>

    int main()
    {
    char a = 'A';
    char b = 'B';

    std::string str = std::string() + a + b;

    return 0;
    }





    share|improve this answer





























      1














      It is possible to create the string in one go. This is the most efficient way also:



      char a = 'A';
      char b = 'B';

      std::string str{a, b};

      std::cout << str << std::endl; // "AB"


      This uses the initializer_list constructor of std::string.





      For a more general way of constructing a string (from any type that works with basic_ostream::operator<<) you can use stringstream:



      char a = 'A';
      char b = 'B';

      std::stringstream ss;

      ss << a << b;

      std::string str2 = ss.str();

      std::cout << str2 << std::endl; // "AB"





      share|improve this answer























      • Most efficient, indeed. But perhaps not what the OP was asking for exactly.
        – not an alien
        Nov 19 '18 at 15:30












      • @notanalien how so?
        – bolov
        Nov 19 '18 at 15:32










      • @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
        – bolov
        Nov 19 '18 at 15:35












      • My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
        – not an alien
        Nov 19 '18 at 15:40












      • @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
        – bolov
        Nov 19 '18 at 16:23





















      -1














      There are following ways..



      #include <bits/stdc++.h>
      #define pb push_back

      using namespace std;

      int main()
      {
      char a = 'A';
      char b = 'B';
      //using string class fill constructor
      string str3;
      str3 = string(1,a) + string(1,b);
      cout << str3;
      str3.clear();
      cout << endl;
      //using stringstream
      stringstream ss;
      ss << a << b;
      str3 = ss.str();
      cout << str3;
      str3.clear();
      cout << endl;
      //using string push_back
      str3.pb(a);
      str3.pb(b);
      cout << str3;
      str3.clear();
      cout << endl;
      //using string::operator+=
      str3+=a;
      str3+=b;
      cout << str3;
      str3.clear();
      cout << endl;
      //using string::append
      str3.append(1,a);
      str3.append(1,b);
      cout << str3;
      str3.clear();
      cout << endl;
      //using string::insert
      str3.insert(0,1,a);
      str3.insert(1,1,b);
      cout << str3;
      str3.clear();
      cout << endl;
      //using string::replace
      str3.replace(0,1,1,a);
      str3.replace(1,1,1,b);
      cout << str3;
      str3.clear();
      cout << endl;
      return 0;
      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377551%2fis-it-possible-to-add-to-char-type-variable-to-get-string%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Sure. There are several ways. Heres a few.



        #include<string>
        #include<iostream>

        using namespace std;

        int main()
        {
        char str1 = 'A';
        char str2 = 'B';
        string str3;
        //using operator+=
        str3 += str1;
        str3 += str2;

        cout << str3 << endl;
        str3.clear();
        //using push_back
        str3.push_back(str1);
        str3.push_back(str2);

        cout << str3 << endl;
        str3.clear();
        //using array access
        str3.resize(2);
        str3[0] = str1;
        str3[1] = str2;
        cout << str3 << endl;

        return 0;
        }





        share|improve this answer




























          4














          Sure. There are several ways. Heres a few.



          #include<string>
          #include<iostream>

          using namespace std;

          int main()
          {
          char str1 = 'A';
          char str2 = 'B';
          string str3;
          //using operator+=
          str3 += str1;
          str3 += str2;

          cout << str3 << endl;
          str3.clear();
          //using push_back
          str3.push_back(str1);
          str3.push_back(str2);

          cout << str3 << endl;
          str3.clear();
          //using array access
          str3.resize(2);
          str3[0] = str1;
          str3[1] = str2;
          cout << str3 << endl;

          return 0;
          }





          share|improve this answer


























            4












            4








            4






            Sure. There are several ways. Heres a few.



            #include<string>
            #include<iostream>

            using namespace std;

            int main()
            {
            char str1 = 'A';
            char str2 = 'B';
            string str3;
            //using operator+=
            str3 += str1;
            str3 += str2;

            cout << str3 << endl;
            str3.clear();
            //using push_back
            str3.push_back(str1);
            str3.push_back(str2);

            cout << str3 << endl;
            str3.clear();
            //using array access
            str3.resize(2);
            str3[0] = str1;
            str3[1] = str2;
            cout << str3 << endl;

            return 0;
            }





            share|improve this answer














            Sure. There are several ways. Heres a few.



            #include<string>
            #include<iostream>

            using namespace std;

            int main()
            {
            char str1 = 'A';
            char str2 = 'B';
            string str3;
            //using operator+=
            str3 += str1;
            str3 += str2;

            cout << str3 << endl;
            str3.clear();
            //using push_back
            str3.push_back(str1);
            str3.push_back(str2);

            cout << str3 << endl;
            str3.clear();
            //using array access
            str3.resize(2);
            str3[0] = str1;
            str3[1] = str2;
            cout << str3 << endl;

            return 0;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 16:38

























            answered Nov 19 '18 at 15:21









            johnathan

            2,158819




            2,158819

























                3














                Yes, it's entirely possible to concatenate char to a std::string, you just need one of the operands to be a std::string, otherwise you are essentially adding integers.



                #include <iostream>

                int main()
                {
                char a = 'A';
                char b = 'B';

                std::string str = std::string() + a + b;

                return 0;
                }





                share|improve this answer


























                  3














                  Yes, it's entirely possible to concatenate char to a std::string, you just need one of the operands to be a std::string, otherwise you are essentially adding integers.



                  #include <iostream>

                  int main()
                  {
                  char a = 'A';
                  char b = 'B';

                  std::string str = std::string() + a + b;

                  return 0;
                  }





                  share|improve this answer
























                    3












                    3








                    3






                    Yes, it's entirely possible to concatenate char to a std::string, you just need one of the operands to be a std::string, otherwise you are essentially adding integers.



                    #include <iostream>

                    int main()
                    {
                    char a = 'A';
                    char b = 'B';

                    std::string str = std::string() + a + b;

                    return 0;
                    }





                    share|improve this answer












                    Yes, it's entirely possible to concatenate char to a std::string, you just need one of the operands to be a std::string, otherwise you are essentially adding integers.



                    #include <iostream>

                    int main()
                    {
                    char a = 'A';
                    char b = 'B';

                    std::string str = std::string() + a + b;

                    return 0;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 15:22









                    not an alien

                    215110




                    215110























                        1














                        It is possible to create the string in one go. This is the most efficient way also:



                        char a = 'A';
                        char b = 'B';

                        std::string str{a, b};

                        std::cout << str << std::endl; // "AB"


                        This uses the initializer_list constructor of std::string.





                        For a more general way of constructing a string (from any type that works with basic_ostream::operator<<) you can use stringstream:



                        char a = 'A';
                        char b = 'B';

                        std::stringstream ss;

                        ss << a << b;

                        std::string str2 = ss.str();

                        std::cout << str2 << std::endl; // "AB"





                        share|improve this answer























                        • Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                          – not an alien
                          Nov 19 '18 at 15:30












                        • @notanalien how so?
                          – bolov
                          Nov 19 '18 at 15:32










                        • @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                          – bolov
                          Nov 19 '18 at 15:35












                        • My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                          – not an alien
                          Nov 19 '18 at 15:40












                        • @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                          – bolov
                          Nov 19 '18 at 16:23


















                        1














                        It is possible to create the string in one go. This is the most efficient way also:



                        char a = 'A';
                        char b = 'B';

                        std::string str{a, b};

                        std::cout << str << std::endl; // "AB"


                        This uses the initializer_list constructor of std::string.





                        For a more general way of constructing a string (from any type that works with basic_ostream::operator<<) you can use stringstream:



                        char a = 'A';
                        char b = 'B';

                        std::stringstream ss;

                        ss << a << b;

                        std::string str2 = ss.str();

                        std::cout << str2 << std::endl; // "AB"





                        share|improve this answer























                        • Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                          – not an alien
                          Nov 19 '18 at 15:30












                        • @notanalien how so?
                          – bolov
                          Nov 19 '18 at 15:32










                        • @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                          – bolov
                          Nov 19 '18 at 15:35












                        • My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                          – not an alien
                          Nov 19 '18 at 15:40












                        • @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                          – bolov
                          Nov 19 '18 at 16:23
















                        1












                        1








                        1






                        It is possible to create the string in one go. This is the most efficient way also:



                        char a = 'A';
                        char b = 'B';

                        std::string str{a, b};

                        std::cout << str << std::endl; // "AB"


                        This uses the initializer_list constructor of std::string.





                        For a more general way of constructing a string (from any type that works with basic_ostream::operator<<) you can use stringstream:



                        char a = 'A';
                        char b = 'B';

                        std::stringstream ss;

                        ss << a << b;

                        std::string str2 = ss.str();

                        std::cout << str2 << std::endl; // "AB"





                        share|improve this answer














                        It is possible to create the string in one go. This is the most efficient way also:



                        char a = 'A';
                        char b = 'B';

                        std::string str{a, b};

                        std::cout << str << std::endl; // "AB"


                        This uses the initializer_list constructor of std::string.





                        For a more general way of constructing a string (from any type that works with basic_ostream::operator<<) you can use stringstream:



                        char a = 'A';
                        char b = 'B';

                        std::stringstream ss;

                        ss << a << b;

                        std::string str2 = ss.str();

                        std::cout << str2 << std::endl; // "AB"






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 19 '18 at 15:31

























                        answered Nov 19 '18 at 15:28









                        bolov

                        30.7k668128




                        30.7k668128












                        • Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                          – not an alien
                          Nov 19 '18 at 15:30












                        • @notanalien how so?
                          – bolov
                          Nov 19 '18 at 15:32










                        • @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                          – bolov
                          Nov 19 '18 at 15:35












                        • My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                          – not an alien
                          Nov 19 '18 at 15:40












                        • @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                          – bolov
                          Nov 19 '18 at 16:23




















                        • Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                          – not an alien
                          Nov 19 '18 at 15:30












                        • @notanalien how so?
                          – bolov
                          Nov 19 '18 at 15:32










                        • @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                          – bolov
                          Nov 19 '18 at 15:35












                        • My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                          – not an alien
                          Nov 19 '18 at 15:40












                        • @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                          – bolov
                          Nov 19 '18 at 16:23


















                        Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                        – not an alien
                        Nov 19 '18 at 15:30






                        Most efficient, indeed. But perhaps not what the OP was asking for exactly.
                        – not an alien
                        Nov 19 '18 at 15:30














                        @notanalien how so?
                        – bolov
                        Nov 19 '18 at 15:32




                        @notanalien how so?
                        – bolov
                        Nov 19 '18 at 15:32












                        @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                        – bolov
                        Nov 19 '18 at 15:35






                        @notanalien "would it be possible to do addition in a somewhat similar fashion [...]? If not, could anyone suggest me some smart workarounds if possible?" how is this not what the OP was asking for? And even if the OP didn't ask for it, it's a good way to achieve this. Remember this site doesn't benefit the OP only.
                        – bolov
                        Nov 19 '18 at 15:35














                        My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                        – not an alien
                        Nov 19 '18 at 15:40






                        My interpretation of "do addition in a somewhat similar fashion" was along the lines of "concatenate std::strings and chars using operator+(), but after re-reading the question I am not so certain. My mistake.
                        – not an alien
                        Nov 19 '18 at 15:40














                        @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                        – bolov
                        Nov 19 '18 at 16:23






                        @notanalien the idea is indeed to concatenate chars into a string. That is the operation asked for. Now this can be achieved in C++ several ways. std::string::operator+, std::string::push_back, std::ostream::operator<< etc. The fact that the op was familiar with one way std::string::operator+ doesn't mean we need to limit ourselves to just finding an operator+.
                        – bolov
                        Nov 19 '18 at 16:23













                        -1














                        There are following ways..



                        #include <bits/stdc++.h>
                        #define pb push_back

                        using namespace std;

                        int main()
                        {
                        char a = 'A';
                        char b = 'B';
                        //using string class fill constructor
                        string str3;
                        str3 = string(1,a) + string(1,b);
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using stringstream
                        stringstream ss;
                        ss << a << b;
                        str3 = ss.str();
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using string push_back
                        str3.pb(a);
                        str3.pb(b);
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using string::operator+=
                        str3+=a;
                        str3+=b;
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using string::append
                        str3.append(1,a);
                        str3.append(1,b);
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using string::insert
                        str3.insert(0,1,a);
                        str3.insert(1,1,b);
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        //using string::replace
                        str3.replace(0,1,1,a);
                        str3.replace(1,1,1,b);
                        cout << str3;
                        str3.clear();
                        cout << endl;
                        return 0;
                        }





                        share|improve this answer




























                          -1














                          There are following ways..



                          #include <bits/stdc++.h>
                          #define pb push_back

                          using namespace std;

                          int main()
                          {
                          char a = 'A';
                          char b = 'B';
                          //using string class fill constructor
                          string str3;
                          str3 = string(1,a) + string(1,b);
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using stringstream
                          stringstream ss;
                          ss << a << b;
                          str3 = ss.str();
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using string push_back
                          str3.pb(a);
                          str3.pb(b);
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using string::operator+=
                          str3+=a;
                          str3+=b;
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using string::append
                          str3.append(1,a);
                          str3.append(1,b);
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using string::insert
                          str3.insert(0,1,a);
                          str3.insert(1,1,b);
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          //using string::replace
                          str3.replace(0,1,1,a);
                          str3.replace(1,1,1,b);
                          cout << str3;
                          str3.clear();
                          cout << endl;
                          return 0;
                          }





                          share|improve this answer


























                            -1












                            -1








                            -1






                            There are following ways..



                            #include <bits/stdc++.h>
                            #define pb push_back

                            using namespace std;

                            int main()
                            {
                            char a = 'A';
                            char b = 'B';
                            //using string class fill constructor
                            string str3;
                            str3 = string(1,a) + string(1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using stringstream
                            stringstream ss;
                            ss << a << b;
                            str3 = ss.str();
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string push_back
                            str3.pb(a);
                            str3.pb(b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::operator+=
                            str3+=a;
                            str3+=b;
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::append
                            str3.append(1,a);
                            str3.append(1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::insert
                            str3.insert(0,1,a);
                            str3.insert(1,1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::replace
                            str3.replace(0,1,1,a);
                            str3.replace(1,1,1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            return 0;
                            }





                            share|improve this answer














                            There are following ways..



                            #include <bits/stdc++.h>
                            #define pb push_back

                            using namespace std;

                            int main()
                            {
                            char a = 'A';
                            char b = 'B';
                            //using string class fill constructor
                            string str3;
                            str3 = string(1,a) + string(1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using stringstream
                            stringstream ss;
                            ss << a << b;
                            str3 = ss.str();
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string push_back
                            str3.pb(a);
                            str3.pb(b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::operator+=
                            str3+=a;
                            str3+=b;
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::append
                            str3.append(1,a);
                            str3.append(1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::insert
                            str3.insert(0,1,a);
                            str3.insert(1,1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            //using string::replace
                            str3.replace(0,1,1,a);
                            str3.replace(1,1,1,b);
                            cout << str3;
                            str3.clear();
                            cout << endl;
                            return 0;
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 '18 at 18:27

























                            answered Nov 19 '18 at 15:28









                            code_cody97

                            709




                            709






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377551%2fis-it-possible-to-add-to-char-type-variable-to-get-string%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                Npm cannot find a required file even through it is in the searched directory