Tuesday, July 31, 2018

Palindrome String and Int Algorithm

Palindrome String :- Palindromes can be read in both directions. How can you determine if a string is a palindrome in the C# language?

 Below is the code to find string is palindrome 

public static void PalindromeStr(string source)
        {
            var reverse = source.ToCharArray();
            Array.Reverse(reverse);
            if(new string(reverse) == source)
                Console.WriteLine(true);
        }

Palindrome Int :- Palindromic number or numeral Palindrome is a number, which remains the same when its digits are reversed. Like 1234321, for example, it is “symmetrical”.

To check if number is palindrome, Use below code

public static void PalindromeInt(int source)
        {
            var value = source;
            int reverse = 0;
            while (value > 0 )
            {
                int remainder = value % 10;
                reverse = (reverse * 10) + remainder;

                value = value / 10;
            }

            if(reverse == source)
                Console.WriteLine(true);
        }



No comments:

Post a Comment

Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...