0

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?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
lucas
  • 19
  • 4
  • 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 Answers1

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
  • 66,800
  • 33
  • 152
  • 256