How can I turn intigers to strings and reverse? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I turn intigers to strings and reverse?

Thanks to all! 🤗😃

20th Feb 2020, 1:16 PM
Barbu Vulc
Barbu Vulc - avatar
2 Answers
+ 11
You can convert integer to string by using built-in function to_string or you can try by yourself. Example: #include <iostream> // std::cout #include <algorithm> // std::reverse() #include <cstring> // std::string using namespace std; string itos(int val){ string tmp; while(0 < val){ tmp.push_back(val%10+48); val /= 10; } reverse(begin(tmp), end(tmp)); return tmp; } int main() { string temp = itos(420); cout<<temp; // outputs 420 return 0; }
20th Feb 2020, 2:54 PM
Vinay_GB
Vinay_GB - avatar
+ 8
There is a function to_string. Take a look here: http://www.cplusplus.com/reference/string/to_string/
20th Feb 2020, 1:20 PM
HonFu
HonFu - avatar