• 0

[C#] Find Nearest Palindrome


Question

Hi All,

I am creating a console application to find the nearest palindrome number to the one submitted, for the life of me I cannot figure out how to make it find the next highest palindrome it will return the same number if that is a palindrome and also will literally do the closest even if it is less.

I would really appreciate any help.

namespace PalidromeC
{
    class Program
    {
        static public void Main(string[] args)
        {
            int palindrome = NearPalindromeFinder.findNearPalindrome(101);
            Console.WriteLine("Nearest Palindrome = " + palindrome);
        }

    }

    public class NearPalindromeFinder
    {
        public static int findNearPalindrome(int start)
        {
            if (testPalindrome(start))
                return start;
            else
            {
                int neg = start;
                int pos = start;
                for (int i = 0; i < start; i++)
                {
                    if (testPalindrome(start + i))
                    {
                        pos = start + i;
                        break;
                    }                                                       
                }
                return (start == neg) ? pos : neg;
            }
        }

        public static string ReverseString(string s)
        {
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }


        private static bool testPalindrome(int start) 
         {
           if (start == 0 || start == 1)
            return true;
           String str = Convert.ToString(start);
           String rev = ReverseString(str);     
           if (str.Equals(rev))
             return true;
           else
             return false;
         }

    }


}	

Link to comment
https://www.neowin.net/forum/topic/884304-c-find-nearest-palindrome/
Share on other sites

7 answers to this question

Recommended Posts

  • 0
  On 17/03/2010 at 12:27, njlouch said:

Quite clearly stated:

I am creating a console application to find the nearest palindrome number to the one submitted

Quite clearly:

An example serves to clarify objectively a textual description

han84, you don't really need ReverseString if you are using C#. Is there no in-built function for this? Also, why don't you look at code where you copied it from: http://stackoverflow.com/questions/2455411/find-the-closest-palindrome-number-c . It might give you some ideas.

  • 0
  On 17/03/2010 at 12:37, Jebadiah said:

Quite clearly:

An example serves to clarify objectively a textual description

han84, you don't really need ReverseString if you are using C#. Is there no in-built function for this? Also, why don't you look at code where you copied it from: http://stackoverflow.com/questions/2455411/find-the-closest-palindrome-number-c . It might give you some ideas.

The code that you quoted is mine, I also asked for help on that site, still had no luck with any answers. I have no idea if there is an in-built function, I'm only a beginner and at the moment am unsure of how to re-work this code so that it ignores if you start on a palindrome number.

  • 0

The code you pasted in your OP works.

To ignore the first number,

(A) just do start++ when you enter the function findNearPalindrome. Clearly you want to ignore the first number no matter what. start++ increments value in start by 1.

OR

(B) Start your for loop from i=1.

However, you can make this more efficient by using the following method:

If your original number is say 9847, the next palindrome will be 98__ ... 98_9 ... 9889.

If your original number is say 37284, the next palindrome will be 37___ ...37__3 ... 37_73 ... 37373 (not 37273 because you want the next palindrome number).

This method makes it faster. You won't need to use the testPalindrome function. Think about improvements.

Also, please comment your code. It's hard to understand because anyone other than you will have a hard time understanding what "pos", "neg", "start", etc. mean.

  • 0
  On 17/03/2010 at 19:35, Jebadiah said:

The code you pasted in your OP works.

To ignore the first number,

(A) just do start++ when you enter the function findNearPalindrome. Clearly you want to ignore the first number no matter what. start++ increments value in start by 1.

OR

(B) Start your for loop from i=1.

However, you can make this more efficient by using the following method:

If your original number is say 9847, the next palindrome will be 98__ ... 98_9 ... 9889.

If your original number is say 37284, the next palindrome will be 37___ ...37__3 ... 37_73 ... 37373 (not 37273 because you want the next palindrome number).

This method makes it faster. You won't need to use the testPalindrome function. Think about improvements.

Also, please comment your code. It's hard to understand because anyone other than you will have a hard time understanding what "pos", "neg", "start", etc. mean.

Thank you so much that's a massive help, and I will put some comments in. Thanks again!

  • 0
  On 18/03/2010 at 08:42, han84 said:

Thank you so much that's a massive help, and I will put some comments in. Thanks again!

You're welcome. There's an efficient algorithm explained on this page for determining the next palindrome number of a given number. It takes care of various scenarios which my example above doesn't.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.