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 124 views
1 answer 134 views
1 answer 114 views
114 views asked May 27, 2023 by avibootz
1 answer 294 views
5 answers 549 views
549 views asked May 10, 2021 by avibootz
1 answer 176 views
6 answers 511 views
511 views asked Aug 10, 2019 by avibootz
...