Check if number/string is palindrome in C#
A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward. In C#, there are several ways to write a program to check whether a given string is a palindrome or not. Here are four possible methods:
Using a loop
One way to check if a string is a palindrome in C# is to use a loop to iterate through the characters of the string and compare the first and last characters, the second and second to last characters, and so on.
This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. It uses a for loop to iterate through the characters of the string up to its halfway point. It compares the first character to the last character, the second character to the second to last character, and so on. If any pair of characters is not equal, the function returns false. If all pairs of characters are equal, the function returns true.
Using recursion
Another way to check if a string is a palindrome in C# is to use recursion. This method is not very efficient, but it is an interesting example of how recursion can be used to solve simple problems.
This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. If the length of the string is 1 or less, the function returns true. Otherwise, it checks whether the first and last characters of the string are equal, and then recursively calls itself with the substring of the original string that excludes the first and last characters. If all pairs of characters are equal, the function returns true.
Using LINQ
You can also use the LINQ (Language Integrated Query) library to check if a string is a palindrome in C#.
This method defines a static function called IsPalindrome() that takes a string as its argument and returns true if the string is a palindrome and false otherwise. It uses the SequenceEqual() method provided by the LINQ library to compare the original string with a reversed version of the string. If the two sequences are equal, the function returns true.
Using string manipulation
You can check if a given string is a palindrome using string manipulation methods. Following is an example of how to write a palindrome program using string manipulation in C#:
This program defines a static function called IsPalindrome() that takes a string as its input and returns a boolean value indicating whether or not the input string is a palindrome. The function first initializes an empty string called reversedStr to store the reversed input string. Then, using a for loop, it iterates over the characters of the input string in reverse order and appends each character to reversedStr. Finally, it compares the input string with the reversed string using the Equals() method and the StringComparison.OrdinalIgnoreCase option, which performs a case-insensitive comparison of the two strings.
In the main method, the program calls IsPalindrome() with the string "radar" and stores the result in the boolean variable isPalindrome. The program then outputs the value of isPalindrome, which is true since "radar" is a palindrome.
- Advantages of C#
- C# vs. Java: What Are Its Main Differences
- Advantages of C# over Python
- First C# Program | Hello World
- Difference between Console.Write and Console.WriteLine in C#
- How do I create a MessageBox in C#?
- C# Comments
- How to reverse a string in C#
- Fibonacci Series Program in C# with Examples
- C# Program to Print Number Triangle
- Get Integer Input from User in C# Console Application
- C# StringBuilder | Learn To Use C# StringBuilder Class
- C# HashMap (Dictionary)
- Ternary Operator (? :) in C# with Examples
- How To Sort Datatable in C#
- Struct Vs Class in C# | Differencees and Advantages
- Async And Await In C#
- IEnumerable in C# | Examples
- ref Vs out in C#
- How to remove item from list in C#?
- How to Add Items to a C# List
- C# StreamWriter Examples
- StreamReader in C# |Examples
- C# Map Example
- Static Method In C# | Examples
- How to convert an Enum to a String in C#
- How to convert a string to an enum in C#
- How to filter a list in C#?