Question about numbers and strings

So as you guys may or may not know, there's a Project Euler task that has you find the largest palindrome of 2 three digit numbers multiplied by each other. Are you able to convert a number into a string, (because strings can be used as arrays and you can check if it's a palindrome by checking for instance, a[0] == a[3]) and then convert that string back into a number?
You can also declare integer arrays. Not just strings.
Example:

int array[];
You can do the same with integers, an array can be of any type.
1
2
3
4
5
6
int arr1[2] = {2,3};
int arr2[2] = {2,5};

if(arr1[0] == arr2[0]) {
    cout << "Yep" << endl;
}

Last edited on
http://www.cplusplus.com/reference/string/stoi/
http://www.cplusplus.com/reference/string/to_string/

pre c++11
1
2
3
4
5
6
7
8
9
10
11
12
std::string to_string(int n){
   std::ostringstream s;
   s << n;
   return s.str();
}

int stoi(std::string s){
   std::istringstream ss(s);
   int n;
   ss>>n;
   return n;
}


no conversion
1
2
3
4
5
6
7
8
9
int reverse(int n){
   int result = 0;
   while(n){
      result *= 10;
      result += n%10;
      n /= 10;
   }
   return result;
}

The integer arrays don't work, because you cannot just do a[0] (2) + a[1] (3) and get 23.
What is a[0](2) + a[1](3)?? I've never seen this syntax in c++ are you trying to say:

a[0]*2+a[1]*3? Because you can very much do that as long as you are passing it to a similar data type to that array
Topic archived. No new replies allowed.