Thursday, January 20, 2011

String Palindrome

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palindrome
{
///
/// To check whether a word or a sentence is a palindrome or not.
/// A palindrome is a word or a sentence that reads the same way from left to right or from right to left.
///

class Program
{
static void Main ( string [ ] args )
{
Console.WriteLine ( "Enter the word or sentence to check Palindrome : " );
string strInput = Console.ReadLine ( );

//traverse through the string with a index at right and left, incrementing and
//decrementing respectively until both value is equal.

int iRightCounter = strInput.Length - 1;
bool bFlag = true;

for ( int iLeftCounter = 0; iLeftCounter < strInput.Length; iLeftCounter++, iRightCounter-- )
{
if ( strInput [ iLeftCounter ] != strInput [ iRightCounter ] )
{
bFlag = false;
break;
}
}

if ( bFlag )
Console.WriteLine ( "String is a Palindrome" );
else
Console.WriteLine ( "Not a Palindrome" );

Console.ReadLine ( );
}
}
}

No comments:

Post a Comment