Check if a string is Palindrome in CSharp

Description

The following code shows how to check if a string is Palindrome.

Example


using System;/*from   w w  w  .  j  a v a2  s  . c  o m*/
using System.Text;

public class MainClass {
    public static bool IsPalindrome(string s) {
        int iLength, iHalfLen;
        iLength = s.Length - 1;
        iHalfLen = iLength / 2;
        for (int i = 0; i <= iHalfLen; i++) {
            if (s.Substring(i, 1) !=
                s.Substring(iLength - i, 1)) {
                return false;
            }
        }
        return true;
    }

    static void Main(string[] args) {
        string[] sa = new string[]{"level", "minim", "radar"};

        foreach (string v in sa)
            Console.WriteLine("{0}\t{1}",v, IsPalindrome(v));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var