3
Reply

Write an algorithm and program to find the word is palindrome or not?

Sibeesh Venu

Sibeesh Venu

10y
2.6k
0
Reply

    public static bool CheckIfPalindrome(string original) { var reversed = new string(original.ToCharArray().Reverse().ToArray()); return original == reversed; }MessageBox.Show(CheckIfPalindrome("MADAM").ToString());

    very easily we can do itpublic static bool CheckIfPalindrome(string original){var reversed = new string(original.ToCharArray().Reverse().ToArray());return original == reversed;}MessageBox.Show(CheckIfPalindrome("MADAM").ToString());

    We can do it in two waysa) By using inbuild functionsstring strRev,strReal = null;Console.WriteLine("Enter the string..");strReal = Console.ReadLine();char[] tmpChar = strReal.ToCharArray();Array.Reverse(tmpChar);strRev=new string(tmpChar);if(strReal.Equals(strRev, StringComparison.OrdinalIgnoreCase)){Console.WriteLine("The string is pallindrome");}else{Console.WriteLine("The string is not pallindrome");}Console.ReadLine(); Ref : http://www.codeproject.com/Tips/153399/To-check-string-is-palindrome-or-not-in-NET-C b)Without Using inbuild functionsWhen i write the first program, the interviewer asked me to write the same by not using inbuild functionsprivate static bool chkPallindrome(string strVal){try{int min = 0;int max = strVal.Length - 1;while (true){if (min > max)return true;char minChar = strVal[min];char maxChar = strVal[max];if (char.ToLower(minChar) != char.ToLower(maxChar)){return false;}min++;max--;}}catch (Exception){throw;}} Ref : You can find out more here http://www.dotnetperls.com/palindrome