Home > Article > Backend Development > Palindrome in C#
Every college or university tends to give some exercise to its students. To make fun, the programs like palindrome are given to the students. Every programming language has its own way to write a program for palindrome. Many people may or may not be familiar with this concept. Do not worry about it. In the coming palindrome in c# article, you will understand what exactly palindrome is and how we can implement it programmatically.
Let’s talk about what is exactly palindrome means. Any number which is the same even if we make it reverse.
Ex: 1234321 => This number, even if you try to write in reverse order, gives you the same no.
More examples are 565,757, 99899, 52125 etc.
Now, let’s look at the exact Method to calculate it.
Suppose we have a no 434.
Now, let’s reverse it, and we get 434 only.
The funniest thing about palindrome is that if we add them, we will get a new palindrome no.
Ex: 214 + 412 = 626 (626 is nothing but the palindrome no.)
That simply means, if we are traversing no forward and backward, then also we are getting the same no.
There are certain ways to check palindrome in different languages. Here we are going to check with C#.
Simple logic used here traverses the serried of input backward and forward, and the given number or a string is the same as the initial than that corresponding output called a palindrome.
Look at the below program carefully, which takes the input from the user and checks whether it is a palindrome or not. This is very simple to identify once you know the concept of a palindrome.
Code:
using System; public class IsPalindrome { public static void Main(string[] args) { int no,total=0,t,rev; Console.Write("Please provide the no: "); no = int.Parse(Console.ReadLine()); t=no; while(no>0) { rev=no%10; total=(total*10)+rev; no=no/10; } if(t==total) Console.Write("The given number is Palindrome."); else Console.Write("The given number is not Palindrome"); } }
Output:
The output which asks for the user to enter any no.
Now, after entering no, it shows the no is a palindrome or not.
In the above program, we took input from the user. We converted that input into the integer by parse method. With the help of a while loop, we reversed the no and checked it with the original no.
If the no provided by the user and the no which we reverser are the same, then that no is a palindrome. Here in our case, we took 212. And the output we got as the given number is a palindrome.
Till now, we have checked only numbers that are a palindrome. But let me tell you we can check this with string also. Because of some combination of strings also has nature like a palindrome.
Let’s take this string, madam and reverse it, and we will find the same string.
It will be good to see by programmatically.
Code:
using System; public class Palindrome { public static void Main(string[] args) { string inputStr, r; inputStr = "Madam"; char[] letter = inputStr.ToCharArray(); Array.Reverse(letter); r = new string(letter); bool bool1 = inputStr.Equals(r, StringComparison.OrdinalIgnoreCase); if (bool1 == true) { Console.WriteLine("This string " + inputStr + " is a Palindrome!"); } else { Console.WriteLine("This string " + inputStr + " is not a Palindrome!"); } Console.ReadLine(); } }
Output:
Now, Let’s talk about the above program. What we actually did here.
We took one string here, which is nothing but the “Madam”. Then we converted that string into a character array. We used the ToCharArray() method to make a character array. After creating a character array, we have used the reverse method on that array to make it reverse. Then we compared both the given string and reversed string with the help of Equals() methods. We stored in the output of this method in a variable called bool1, which is of type Boolean.
Now, at the last, with the help of the if-else condition, we checked for the variable bool1 is equal to true or false. In our program, the if condition gets executed. Hence the output is “The string Madam is a palindrome.”
There is no limitation on finding the words or numbers.
You can make puzzles out of it. There are many conditions, like if you have one whole paragraph and someone asks you to check if there is any string that is a palindrome or not.
Some examples are like they have a paragraph, but the whole sentence itself is a palindrome.
If we reverse the above sentence, then we get the same sentence again.
Let’s take an example; Please check out the below code.
Code:
using System; public class Palindrome { public static bool IsPalindrome(string value) { int x = 0; int y = value.Length - 1; while (true) { if (x > y) { return true; } char i = value[x]; char j = value[y]; // Scan forward for a while invalid. while (!char.IsLetterOrDigit(i)) { x++; if (x > y) { return true; } i = value[x]; } while (!char.IsLetterOrDigit(j)) { y--; if (x > y) { return true; } j = value[y]; } if (char.ToLower(i) != char.ToLower(j)) { return false; } x++; y--; } } public static void Main() { string[] ary = { "Was it a car or a cat I saw?" }; foreach (string value in ary) { Console.WriteLine("{0} = {1}", value, IsPalindrome(value)); } } }
Output:
Hence, as we talked about, the whole sentence itself is a palindrome. If you reverse it also, it will give you the same sentence.
In the above program, we traverse through the sentence till the last. Then we made it to lowercase and checked against the given sentence. Try this program by coding yourself; then only you can understand the actual flow of these programs.
There are many tricks like a palindrome. We have seen the sting and number, which are palindrome in nature. How easily, with some steps, we achieved it.
The above is the detailed content of Palindrome in C#. For more information, please follow other related articles on the PHP Chinese website!