Find the closest palindrome number C#

Posted by user294837 on Stack Overflow See other posts from Stack Overflow or by user294837
Published on 2010-03-16T14:59:01Z Indexed on 2010/03/16 15:01 UTC
Read the original article Hit count: 259

Filed under:

Hi All,

I am trying to create a console application that reads number from a file all underneath each other like so:

101 9

and then outputs into another file the closest palindrome number. So far what I have is not quite rightm i.e. I don't think I can put the class inside a method which is a bit more Java I was wandering if anyone could help at all?

Thanks :)

namespace PalidromeC
{
    class Program
    {
        static public void Main(string[] args)
        {

          #region WriteAnswers

         try
         {
            StreamWriter sw = new StreamWriter("C://Temp/PalindromeAnswers.txt");
            sw.WriteLine("Answers");
            sw.Close();
        }//try
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }//catch
        #endregion

        #region ReadFile
        try
        {
            string numbers;

            StreamReader sr = new StreamReader("C://Temp/Palindrome.txt");

            numbers = sr.ReadLine();

            while (numbers != null)
            {
                Console.WriteLine(numbers);
                numbers = sr.ReadLine();
            }//while
            sr.Close();
            Console.ReadLine();

        }//try
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }//catch

        #endregion


        NearPalindromeFinder f = new NearPalindromeFinder();
                int palindrome = f.findNearPalindrome(n);
                Console.WriteLine("Nearest Palindrome = " + palindrome);

    }//main

    public static void testFindNearestPalindrome(int n)
    {
            class NearPalindromeFinder 
        {
                        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)) 
                        {
                                                        neg = start-i;
                                                        break;
                                                }//if
                                                if (testPalindrome(start+i)) 
                        {
                                                        pos = start+i;
                                                        break;
                                                }//if                                                       
                                        }//for
                                        return (start == neg) ? pos : neg;
                                 }//else
             }//findNearPalindrome


                        bool testPalindrome(int start) 
                {
                                    if (start == 0 || start == 1)
                                            return true;
                                    String str = String.valueOf(start);
                                    String rev = new        
                                    if (str.equals(rev))
                                            return true;
                                    else
                                            return false;
                }//testPalindrome

        }//NearPalindromeFinder class
    }//testFindNearestPalindrome

}//Program Class 

© Stack Overflow or respective owner

Related posts about c#