How to convert arrays to strings? In cpp | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to convert arrays to strings? In cpp

28th Oct 2019, 2:23 PM
bavan
bavan - avatar
3 Answers
+ 1
What array are you talking about? you need to be more descriptive with your question. Otherwise it will be hard to expect an effective and efficient discussion. See and follow this guide to posting a question 👍 https://www.sololearn.com/Discuss/333866/?ref=app
28th Oct 2019, 3:30 PM
Ipang
+ 2
For example char x[5]={a,b,c,d,e} --> string x="abcde"
28th Oct 2019, 6:14 PM
bavan
bavan - avatar
+ 2
Paste this snippet into an empty main function, and observe the output. I'm sure there are other ways, this is just what I know. char ca1[5] {'a','b','c','d','e'}; char ca2[6] {'a','b','c','d','e','\0'}; char ca3[] {"fghij"}; const char* ccp {"vwxyz"}; // string from char array ca1 std::cout << std::string(ca1) << std::endl; // string from char array ca2 std::cout << std::string(ca2) << std::endl; // string from char array ca3 std::cout << std::string(ca3) << std::endl; // string from const char pointer ccp std::cout << std::string(ccp);
28th Oct 2019, 6:56 PM
Ipang