A palindrome string is the one that once reversed produces the same result as the original input string. Let us proceed with an example for the same.
Write the following code in a console application.
- static void Main(string[] args)
- {
- string _inputstr, _reversestr = string.Empty;
- Console.Write("Enter a string : ");
- _inputstr = Console.ReadLine();
- if (_inputstr != null)
- {
- for (int i = _inputstr.Length - 1; i >= 0; i--)
- {
- _reversestr += _inputstr[i].ToString();
- }
- if (_reversestr == _inputstr)
- {
- Console.WriteLine("String is Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
- }
- else
- {
- Console.WriteLine("String is not Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
- }
- }
- Console.ReadLine();
- }
Let us check the output now. I would be using a palindrome string first to check if the logic is working properly. And in the second case a non palindrome string to verify the same. Since my name is palindrome so i would be using it to verify it first.
Output