0

I made this code that is supposed to increment a number till get the next palindrome number of of this inputted number.

The program take the number as string " cause it may be a very big digits number ( 0 < digits < 1000000 ) " ....

The Code

int main ()
{
string number = "1243";
int position = number.length()-1;
do
{
    if (number[position] == '9')
    {
        //cout << "hereee";
        number[position] = '0';
        int n1 = (int)number[position-1] - '0';
        n1++;
        number[position-1] = n1 + '0';
        nextPalindrome[position-1];
         cout << number <<"hereee2"<< endl; // only to determine if i get in "if"
    }
    else
    {
        int n1 = (int)number[position] - '0';
        n1++;
        number[position] = n1 + '0';
        cout << number <<"hereee1" << endl; // only to determine if i get in "else"
    }
} while (isPalindrome(number) == false);
}

It's start to take the digit in the current position and increment it and return it as character again

The Problem

cout << number <<"hereee1" << endl;

this line show the number status while runing and it is like that :

12"6 hereee1

12"7 hereee1

12"8 hereee1

12"9 hereee1

12#0 hereee2

12#1 hereee1

while it must to be

1236 hereee1

1237 hereee1

1238 hereee1

1239 hereee1

1240 hereee2

1241 hereee1

i don't know where is the error .. can any one help

NOTE : "isPalindrome" is a function take a string as parameter and if the original string equal to its reverse, it returns true .. else return false

Mahmoud
  • 197
  • 1
  • 5
  • 16
  • I think there are nicer approaches to this than brute force... – ppeterka Jan 29 '13 at 12:45
  • I know there is a lot of ways.. but this a way that i involved in i must find the error reason – Mahmoud Jan 29 '13 at 12:48
  • you do realise that doing it this way is 1, unelegant 2, inefficient 3, easy to do wrong and 4, creates problems that wouldn't exist if you did it with being smart? – ppeterka Jan 29 '13 at 13:02
  • I am so beginner you know so told me adiiferent way to make it and thanks for advance ^_^ – Mahmoud Jan 29 '13 at 13:27
  • 1
    You could go with a logic that starts from the middle and goes to the outer edges. If the digit on the right side is greater than the one on the left, add 1 to the left. Then put the same digit to the right position as it is on the left. Repeat until done. Only one special case needs to be handled: if the starting number is full of only `9`s, the next palindrome would be in the form `10<.lot of 0s.>01`. If I have some time, I think I'll write it into the answer for my own pleasure - it is a good brain exercise. – ppeterka Jan 29 '13 at 13:41
  • i get it ... and i will make it ...... Thanks Alot ^_^ – Mahmoud Jan 29 '13 at 13:48
  • A nice explanation here http://www.kodemonk.com/palin-the-next-palindrome/ – codeomnitrix Jun 14 '16 at 07:12

2 Answers2

3

What is the result of this when the digit at position-1 is '9'?

    int n1 = (int)number[position-1] - '0';
    n1++;
    number[position-1] = n1 + '0';
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • this part will not be called when the position-1 is '9' it is only called when the digit at position = '9' ... so it makes it make the digit at position = '0'... and increment the digit at position-1 by one – Mahmoud Jan 29 '13 at 12:56
  • But what happens when the digit at position-1 becomes '9' and from incrementing it and you go around again? – Mats Petersson Jan 29 '13 at 13:00
  • 1
    @Mahmoud Think a bit! What will happen, when the number reaches for example `1299`? – ppeterka Jan 29 '13 at 13:01
  • yes you right ... it makes 9+1 and this will produce wrong char – Mahmoud Jan 29 '13 at 13:05
  • 1
    Yes. I didn't want to give you the whole solution, because you are the one learning programming. I've already been there and done that [a few times]. – Mats Petersson Jan 29 '13 at 13:06
1

You don't handle carryover well... This part is invalid:

    int n1 = (int)number[position-1] - '0';
    n1++;
    number[position-1] = n1 + '0';

This just increases the digit on the previous place. And if it happens to be '9' (same as what Mats Petersson tried to suggest), it just overflows... It should however be carried over to the next digit too... This is a recursive solution to this (beware, there might be syntax errors, I haven't coded in C++ since ages...):

/*
* This function adds one to the specified digit of a 
* string containing a decimal integer.
*
* Contains no checks whatsoever. Behavior is undefined when 
* not supplied a valid input string.
*/
int addOneToDigit (string number, int digit)
{
    if (number[digit] == '9')
    {
        number[digit] = '0';
        //we need to handle getting a longer string too...
        if(digit>0) 
        {
            return addOneToDigit(number, digit-1);
        }
        else
        {
            return "1" + number;
        }
    }
    else
    {
        int n1 = (int)number[digit] - '0';
        n1++;
        number[digit] = n1 + '0';
    }
    return number;
}

and the main() would look like something like this:

int main ()
{
    string number = "1243";
    do
    {
      number = addOneToDigit(number,number.length()-1)
    } 
    while (isPalindrome(number) == false);
}
ppeterka
  • 20,583
  • 6
  • 63
  • 78