Ways Of Determining If A Word/String Is a Palindrome

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.Palindrome  
  7. {  
  8.     class UsingReverseMethod {  
  9.         static void Main() {  
  10.             Console.WriteLine("Enter the String to check:");  
  11.             String input = Console.ReadLine();  
  12.             string output = string.Empty;  
  13.             char[] inputtochar = input.ToCharArray(); // this line converts the string to char for reversing  
  14.             Array.Reverse(inputtochar); // this line actually reverse the Converted CharArray  
  15.             output = new string(inputtochar); //this line converts the ReversedcharArray back to string  
  16.             if (input == output) {  
  17.                 Console.WriteLine("Palidrome");  
  18.                 Console.ReadLine();  
  19.             } else {  
  20.                 Console.WriteLine("Not palindrome");  
  21.                 Console.ReadLine();  
  22.             }  
  23.         }  
  24.     }  
  25. }using System;  
  26. using System.Collections.Generic;  
  27. using System.Linq;  
  28. using System.Text;  
  29. using System.Threading.Tasks;  
  30. namespace ProgrammingInterviewFAQ.Palindrome  
  31. {  
  32.     class UsingReverseMethod {  
  33.         static void Main() {  
  34.             Console.WriteLine("Enter the String to check:");  
  35.             String input = Console.ReadLine();  
  36.             string output = string.Empty;  
  37.             char[] inputtochar = input.ToCharArray(); // this line converts the string to char for reversing  
  38.             Array.Reverse(inputtochar); // this line actually reverse the Converted CharArray  
  39.             output = new string(inputtochar); //this line converts the ReversedcharArray back to string  
  40.             if (input == output) {  
  41.                 Console.WriteLine("Palidrome");  
  42.                 Console.ReadLine();  
  43.             } else {  
  44.                 Console.WriteLine("Not palindrome");  
  45.                 Console.ReadLine();  
  46.             }  
  47.         }  
  48.     }  
  49. }  
Output

Enter the string to check: Redivider

Palindrome

Enter the String to check: Karthik

Not Palindrome


Way 2: Without using Array. Reverse() Method
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.Palindrome   
  7. {  
  8.     class WithoutUsingReverseMethod {  
  9.         static void Main(string[] args) {  
  10.             Console.WriteLine("Enter the String to check:");  
  11.             String input = Console.ReadLine(); //getting the value(input) from user  
  12.             string output = string.Empty;  
  13.             int inputlength = input.Length; // get the length to perform loop  
  14.             for (int i = 0; i < inputlength;) {  
  15.                 output += input[inputlength - 1].ToString(); //Important :Transfer one string variable to another string variable  
  16.                 //At the specified index  
  17.                 inputlength--; //decrease the index to get the value in reverse order  
  18.             }  
  19.             if (input == output) // finally check with input  
  20.             {  
  21.                 Console.WriteLine("Palidrome");  
  22.                 Console.ReadLine();  
  23.             } else {  
  24.                 Console.WriteLine("Not palindrome");  
  25.                 Console.ReadLine();  
  26.             }  
  27.         }  
  28.     }  
  29. }  
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.

 

Next Recommended Reading Check a Palindrome String in C#