Hi all,

I am currently working on a program which tests whether 2 numbers multiplied produces a PALINDROME.

An example of a palindrome is

16461
which can be read same from both ways hence i devised a test.

#include <iostream>
#include <string>
using namespace std;
char pali(char orig[100]);

int main()
{
  for(int a=100;a<1000;a++)
  {
          for(int b=100;b<1000;b++)
          {
              if(pali(a*b)==1)
              {
                                    cout<<a*b;
                                    break;
              }
          }
  }
}
                  
char pali(char orig[100])
{
     char test[100]
     int c=0;     
     for(int b=strlen(orig);b>=0;b--)
     {
             orig[b]=test[c];
             c++;
     }
     if(strcmp(orig[100],test[100])
     {
        return 1;
     }
     else
     {
         return 0;
     }
}

The problem is that i get errors that i cant convert a int into char. even when i use a cast..

Help me out

Recommended Answers

All 10 Replies

You can use stringstreams, itoa or sprintf to convert your int to a string of chars.

The problem is that i get errors that i cant convert a int into char. even when i use a cast..

The error would have been that you can't convert an int to a char _pointer_, not char. C++ doesn't automagically convert from integer types to string types.

I understand that itoa and sprintf will convert them but can i have an example of exactly how to implement it in my example?

Member Avatar for iamthwee

No no, use stringstreams. Google it.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
char pali(char orig[100]);
char s[100];
int main()
{
  for(int a=100;a<1000;a++)
  {
          for(int b=100;b<1000;b++)
          {
                  ostringstream outs;
                  outs <<a*b ;   // Convert value into a string.
                  s = outs.str();      // Get the created string from the output stream.
              if(pali(s)==1)
              {
                                    cout<<a*b;
                                    break;
              }
          }
  }
}
                  
char pali(char orig[100])
{
     char test[100]
     int c=0;     
     for(int b=strlen(orig);b>=0;b--)
     {
             orig[b]=test[c];
             c++;
     }
     if(strcmp(orig[100],test[100])
     {
        return 1;
     }
     else
     {
         return 0;
     }
}

I have done my experiment and got this code. I get errors stating that i cant convert string to char. what do i do now?

If you want to keep using the array 'char s[100]', you can explicitly copy the
ostringstream's content into the array.
I.e ..

strcpy(s, outs.str().c_str());

Or use the >> operator, i.e.

outs >> s;
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
char pali(char orig[100]);
char s[100];
int main()
{
  for(int a=100;a<1000;a++)
  {
          for(int b=100;b<1000;b++)
          {
                  ostringstream outs;
                  outs <<a*b ;   // Convert value into a string.
                  strcpy(s, outs.str().c_str());      // Get the created string from the output stream.
              if(pali(s)==1)
              {
                                    cout<<a*b<<"\n";
                                    cin.get();
                                    break;
              }
                      }
  }
  cin.get();
  cin.get();
}
                  
char pali(char* s)
{
     char test[100];
     int c=0;     
     for(int b=strlen(s);b>=0;b--)
     {
             s[b]=test[c];
             c++;
     }
     cout<< "Test :: " <<test<<"\n";
     cout<< "Original :: " <<s<< "\n";
     
     if(!(strcmp(s,test)))
     {
        return 1;
     }
     else
     {
         return 0;
     }
}

I got it until here and there are no compiling errors
But the problem now is that the

for(int b=strlen(s);b>=0;b--)
     {
             s[b]=test[c];
             c++;
     }

isnt working fine.
I mean it is not reversing the numbers
And i get some junk in that place.

Take a look at this statement, what is it doing and what do you want it to do? s[b]=test[c]; . Also, what is the value of b the first time through the loop?

In addition: You never close your new string with a '\0' character. This will result in a lot of garbage output, and maybe even a crash.

I also noticed your pali function is defined to return a char, but you are really returning an int.
You may be too commited to this approach, but as an alternative, you could eliminate the need to copy the string and simply use your index variables (b and c) to compare the first char of the array to the last char and while the chars are equal and c <= b, increase c and decrease b.

Thanks for the help. I have figured it out. I just kept the assignemnet wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.