+ 1
Convert array of chars into string using c++.
I have coded a random password generator, but I would like to save the password as a string. Is it possible to convert an array of chars into a string in c++? Thanks.
5 ответов
+ 4
char a[] = "sololearn";
string s = string(a);
cout<<s;
+ 3
Yes, array of characters is a string with ending \0.
try to put \0 in last array index and that can store in string variable.
+ 3
this will work even with uninitialized char array. After you have stored all the char in the array, you just need to write this code at the end to convert it into string.
+ 2
Works like a charm thanks sooo much!
+ 1
@kamal joshi Your code works great. But if i used an array for 'acceptable chars' and a for loop to produce an array that holds random numbers each time you run the code, using srand(time(0)) and rand(), how do I convert that array that wasn't initialized into a string. Or is that even possible?