Software Development Tricky

Thursday, 27 April 2017

Palindrome String in CSharp


Ø  A palindrome has the same letters on both ends of the string. Ex: sagas

Sample Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
namespace Logical_Programs
{
    class palindrome_string
    {
        static void Main()
        {
            string[] array =
            {
                "civic",
                "kani",
                "kayak",
                "routes"
            };
            Console.WriteLine("check string as palindrome or not");
            Console.WriteLine("**********************************");
            foreach (string value in array)
            {
                Console.WriteLine("{0} = {1}", value, isPalindrome(value));
            }
            Console.WriteLine("**********************************");
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
        /// <summary>
        /// check polindrome or not
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static bool isPalindrome(string word)
        {
            int min = 0;
            int max = word.Length - 1;
            while (true)
            {
                if (min > max)
                {
                    return true;
                }
                char a = word[min];
                char b = word[max];
                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                min++;
                max--;
            }
        }
    }
}

Output:


If you like this post. Kindly share your feedback.

No comments:

Post a Comment

Followers