Sunday, January 12, 2020

C# Palindrome Program


palindrome is a word, phrase, number or sequence of words that reads the same backward as forward. For Example Madam, Nitin, RAGGAR etc.
below is a C# program which checks given string is palindrome or not:

program logic : suppose we have a string of length 5, we will check first char of string is equal to last char of string, second char of string is equal to second last char of string and so on. like
char[0]==char[4]
char[1]==char[3]
char[2]==char[2]
if all matched then string is palindrome.

        public static void IsPalindrome(string s)
        {
          
            char[] cArr = s.ToCharArray(); // Converting string to char array
            bool isPalindrome = true; //a flag 
            
            for (int i = 0; i < s.Length / 2; i++)
            {
               // Console.WriteLine("Iteration i=" + i);// to get iteration count

                if (cArr[i] != cArr[(s.Length - 1 - i)])
                {
                    isPalindrome = false;
                    Console.WriteLine("Not Palindrome");
                    break;
                }
                
            }
            if (isPalindrome)
            {
                Console.WriteLine("Palindrome");
            }

        }


C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...