How to convert char to string in C++

6 Answers

0 votes
#include <iostream>
    
using namespace std;


int main() 
{ 
    char ch = 'a';
 
    string s(1, ch); 

    cout << s;
 
    return 0; 
}
 
    
    
    
/*
run:
    
a
    
*/

 



answered Aug 10, 2019 by avibootz
0 votes
#include <iostream>
#include <sstream>
     
using namespace std;
 
 
int main() 
{ 
    char ch = 'a';
  
    string s;
	stringstream ss;
	
	ss << ch;
	ss >> s;	
 
    cout << s;
  
    return 0; 
}
  
     
     
     
/*
run:
     
a
     
*/

 



answered Jan 9, 2020 by avibootz
0 votes
#include <iostream>

using namespace std;
 
 
int main() 
{ 
    char ch = 'a';
  
    string s;
	s.push_back(ch);
 
    cout << s;
  
    return 0; 
}
  
     
     
     
/*
run:
     
a
     
*/

 



answered Jan 9, 2020 by avibootz
0 votes
#include <iostream>

using namespace std;
 
 
int main() 
{ 
    char ch = 'a';
  
    string s;
	s += ch;
 
    cout << s;
  
    return 0; 
}
  
     
     
     
/*
run:
     
a
     
*/

 



answered Jan 9, 2020 by avibootz
0 votes
#include <iostream>

using namespace std;
 
 
int main() 
{ 
    char ch = 'w';
  
    string s;
	s = ch;
 
    cout << s;
  
    return 0; 
}
  
     
     
     
/*
run:
     
w
     
*/

 



answered Jan 9, 2020 by avibootz
0 votes
#include <iostream>

using namespace std;
 
 
int main() 
{ 
    char ch = 'w';
  
    string s;
	s.append(1, ch);
 
    cout << s;
  
    return 0; 
}
  
     
     
     
/*
run:
     
w
     
*/

 



answered Jan 9, 2020 by avibootz

Related questions

1 answer 124 views
1 answer 134 views
1 answer 115 views
115 views asked May 27, 2023 by avibootz
1 answer 295 views
5 answers 550 views
550 views asked May 10, 2021 by avibootz
3 answers 226 views
226 views asked May 9, 2021 by avibootz
1 answer 176 views
...