To analyze every character of a given number, like 2002, to be able to separate them I need to convert it to a string. But, as soon as I do that, how to manage this string?(its name and size). Just like when I need to say if the given number is a palindrome for example, after I convert the number to a string, how to manage this string?
Asked
Active
Viewed 46 times
0
-
2_"how to manage this string?"_ Using [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string). – Jul 01 '17 at 14:12
1 Answers
0
Use to_string()
, like this for example (printing every digit of the number, after converting the number to string, printing every character of the string that is):
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string str = std::to_string(2002);
for(auto c: str)
std::cout << c << " ";
std::cout << std::endl;
return 0;
}
Output:
2 0 0 2
PS: For the palindrome example you mention, please read Check if a string is palindrome.

gsamaras
- 71,951
- 46
- 188
- 305