C# Help!

Discussion in 'Tech Support: Questions & Help' started by Karen, Apr 15, 2010.

  1. Karen

    KarenKaren is a Verified Member Mistress of the Night

    Joined:
    Sep 24, 2009
    Last Seen:
    Tuesday
    Messages:
    619
    Likes Received:
    122
    Hey guys... help me out if you've got some knowledge about C#! :eek:

    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.

    Code:
    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;
             }
    
        }
    
    
    }       
    
    I'm a bit lost, I'm no expert at this stuff!!!

    <3
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. TAG Manager

    Executive Leadership

    Joined:
    Aug 23, 2009
    Last Seen:
    Today
    Messages:
    11,528
    Likes Received:
    23,163
    Why on earth are you asking this here? lol? You're lucky I have the answer you need!

    What you posted works, but 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.

    For the love of god, please comment your code if you intend on putting this out somewhere!

    HTH!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Karen

    KarenKaren is a Verified Member Mistress of the Night

    Joined:
    Sep 24, 2009
    Last Seen:
    Tuesday
    Messages:
    619
    Likes Received:
    122
    I was ignored on another board; I thought I would actually get a response here!

    It's shared code from somewhere else, I'm modifying it to put into something else I'm working. Funny thing this is, my job has nothing to do with programming, yet here I am doing this little side project. Hmm..

    Thanks for your help, I think I've got it figured out now.

    Much <3
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. TAG Manager

    Executive Leadership

    Joined:
    Aug 23, 2009
    Last Seen:
    Today
    Messages:
    11,528
    Likes Received:
    23,163
    Not a problem. :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...

Share This Page