Logical Problem

I know this question has been asked before, but the answers were, with my very limited knowledge of C++, too complicated for me to understand. So I'm asking you, how do I convert an integer to a string?

I mean, this is what I found on this forum, and it doesn't make any sense at all to me:

1
2
int number;
string String = static_cast<ostringstream*>( &(ostringstream() << number) )->str();
Last edited on
Assuming it's a valid integer, here's a reasonably simple way to do it. A bit basic, but might do the trick:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>

template <class T>
std::string ToString( T t )
{
  std::stringstream ss;
  ss << t;
  return ss.str();
}

int main( int argc, char* argv[] )
{
  int a = 52;

  std::string s = ToString<int>( a );

  std::cout << "s is " << s << std::endl;

  return 0;
}


If you're using C++11, use std::to_string.
@ iHutch105

Not very simple for me xD There were a lot of things that I didn't understand there, but I guess I'll just have to memorize it and then later on when I know more about C++ I maybe will be able to understand it. What I was going to use it for was solving Euler Problem #4, and I copied the code that I mentioned above without any understanding of it whatsoever. But it still does not seem to work, do you see what's wrong?

Euler Problem #4: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
using namespace std;
int main(){
    int palindrome;
    for(int x=100;x<1000;x++){
        for(int y=100;y<1000;y++){
            int test=x*y, counter=0;
            string String = static_cast<ostringstream*>( &(ostringstream() << test) )->str();
            for(int z=0;z<String.size();z++){
                if(String[z]==String[String.size()-z-1]){
                    counter++;
                }
            }
            if(counter==String.size()){
                palindrome=test;
            }
        }
    }
    cout << "The largest palindrome made out of two three-digit numbers is: " << palindrome;
}


The program gives me a palindrome, 580 085 , but it's not the right answer.
I can't see at a glance what could be causing it.

That's not the answer I get when I run my version, though. I should mention that I don't use strings at all.

I have a break coming up (currently at work), I'll see if I can take another look at your code then. If you want me to put mine up, I'd be more than happy to. Just don't want to spoil the game for you. :-)
Don't worry, I don't think I'll be able to come up with any other way of solving this right now, so you can post your version if you want to :)
No worries. Here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <ctime>

bool IsPalindrome( int n )
{
  int nCpy = n;         // Copy of n
  unsigned long r = 0;  // n reverse

  while( nCpy > 0 )
  {
    r = r * 10 + nCpy % 10;
    nCpy /= 10;
  }

  return n == r;
}

int main( int argc, char* argv[] )
{
  unsigned long largest = 0;  // Largest palindrome
  unsigned int i, j;          // Loop counters
  unsigned long p;            // Product of i and j
  int first, second;          // Factors
  clock_t t = clock();        // Timer

  for( i=100; i < 1000; ++i )
  {
    for( j=100; j < 1000; ++j )
    {
      p = i * j;
      if( IsPalindrome( p ) )
      {
        if( p > largest )
        {
          largest = p;
          first   = i;
          second  = j;
        }
      }
    }
  }

  t = clock() - t;
  std::cout << "The largest palindrome is " << largest;
  std::cout << " (" << first << " x " << second << ")\n";
  std::cout << "Time taken: " << static_cast<float>( t ) / CLOCKS_PER_SEC << " seconds\n";

  return 0;
}

Answer I get is 906609 (913*993).

There's some timing stuff in the and it could probably be tweaked to be a little faster, but it's reasonably quick.

I'll still take a look at your current code when I get the chance, if only for sheer curiosity.

EDIT: Ideone link to prove output: http://ideone.com/zQBwpD
Last edited on
Wow, yours is a lot faster than mine xD Thanks a lot :)
Yours fixed...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
using namespace std;
int main(){
    int palindrome = 0;
    for(int x=100;x<1000;x++){
        for(int y=100;y<1000;y++){
            int test=x*y, counter=0;
            string String = static_cast<ostringstream*>( &(ostringstream() << test) )->str();
            for(int z=0;z<String.size();z++){
                if(String[z]==String[String.size()-z-1]){
                    counter++;
                }
            }
            if(counter==String.size() && test > palindrome ){
                palindrome=test;
            }
        }
    }
    cout << "The largest palindrome made out of two three-digit numbers is: " << palindrome;
}


Changes:
1) palindrome initialised on line 5
2) test > palindrome check added to line 15
Oh! You added test > palindrome because the loop goes through lower three-digit numbers sometimes than those the current palindrome consists of, right? So the last one the loop met was 580085, but not the largest? I get it. Thanks man :)
Last edited on
Yep, exactly.
A simple way of converting integers to strings.
You can modify to convert it for unsigned integers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string tostr(unsigned int x)
{
string str;
if(x < 0)
{
     str.push_back('-');
     x = -x
}
while(x)
    {
          str.push_back(x%10 + 48); //convert numbers to ascii characters
          x/=10;
    }
}

ihutch...
You shouldn't have posted the answer to the problem and your solution.
Last edited on
> str.push_back(x%10 + 48); //convert numbers to ascii characters

1
2
str.push_back( x%10 + '0' ); // convert number to character 
                             // in the character set used my the implementation 
amhndu wrote:
ihutch...
You shouldn't have posted the answer to the problem and your solution.

I checked with the OP first then posted it after he was happy.

It's a 50-line code segment with "Here it is" at top, so it can be quite easily skipped someone wants to avoid it.

Simply put; don't want, don't read.
Last edited on
Topic archived. No new replies allowed.