How to convert a char to a string in C++

3 Answers

0 votes
#include <iostream>   

int main () {
    char ch = 'z';

    std::string s(1, ch);
    
    std::cout << s;
}
 
 
 
 
/*
run:
 
z
 
*/

 



answered May 9, 2021 by avibootz
edited Mar 28, 2023 by avibootz
0 votes
#include <iostream>   

int main () {
    char ch = 'z';

    std::string s;
    
    s.push_back(ch);
    
    std::cout << s;
}
 
 
 
 
/*
run:
 
z
 
*/

 



answered May 9, 2021 by avibootz
edited Mar 28, 2023 by avibootz
0 votes
#include <iostream>   

int main () {
    char ch = 'z';

    std::string s;
    
    s.append(1, ch);
    
    std::cout << s;
}
 
 
 
 
/*
run:
 
z
 
*/

 



answered May 9, 2021 by avibootz
edited Mar 28, 2023 by avibootz

Related questions

1 answer 130 views
1 answer 145 views
1 answer 118 views
118 views asked May 27, 2023 by avibootz
1 answer 306 views
5 answers 562 views
562 views asked May 10, 2021 by avibootz
1 answer 182 views
6 answers 533 views
533 views asked Aug 10, 2019 by avibootz
...