Introduction
In the below content, you will find simple, different, yet important ways to find whether a string /word is palindrome or not.
Way 1 : Using Array. Reverse () Method
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProgrammingInterviewFAQ.Palindrome
- {
- class UsingReverseMethod {
- static void Main() {
- Console.WriteLine("Enter the String to check:");
- String input = Console.ReadLine();
- string output = string.Empty;
- char[] inputtochar = input.ToCharArray();
- Array.Reverse(inputtochar);
- output = new string(inputtochar);
- if (input == output) {
- Console.WriteLine("Palidrome");
- Console.ReadLine();
- } else {
- Console.WriteLine("Not palindrome");
- Console.ReadLine();
- }
- }
- }
- }using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProgrammingInterviewFAQ.Palindrome
- {
- class UsingReverseMethod {
- static void Main() {
- Console.WriteLine("Enter the String to check:");
- String input = Console.ReadLine();
- string output = string.Empty;
- char[] inputtochar = input.ToCharArray();
- Array.Reverse(inputtochar);
- output = new string(inputtochar);
- if (input == output) {
- Console.WriteLine("Palidrome");
- Console.ReadLine();
- } else {
- Console.WriteLine("Not palindrome");
- Console.ReadLine();
- }
- }
- }
- }
Output
Enter the string to check: Redivider
Palindrome
Enter the String to check: Karthik
Not Palindrome
Way 2: Without using Array. Reverse() Method
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProgrammingInterviewFAQ.Palindrome
- {
- class WithoutUsingReverseMethod {
- static void Main(string[] args) {
- Console.WriteLine("Enter the String to check:");
- String input = Console.ReadLine();
- string output = string.Empty;
- int inputlength = input.Length;
- for (int i = 0; i < inputlength;) {
- output += input[inputlength - 1].ToString();
-
- inputlength--;
- }
- if (input == output)
- {
- Console.WriteLine("Palidrome");
- Console.ReadLine();
- } else {
- Console.WriteLine("Not palindrome");
- Console.ReadLine();
- }
- }
- }
- }
Output
Enter the String to check: Redivider
Palindrome
Enter the String to check: Karthik
Not Palindrome
Conclusion
As a developer, even after we have experience, when it comes to preparation for an interview, there is always a necessity of taking a look at this kind of simple program. And, there are different ways of achieving the same thing. So I thought to put those in one place which we can easily go through.
In the future, I will update this blog if I get any other ways to achieve the same. The source code is in the attachment section.
I hope it will be helpful, especially for job seekers; kindly let me know your thoughts or feedback.