share this page !

search

Sunday, June 13, 2010

JAVA : Program to check if a word(string) is palindrome or not.


Here are the Mumbai University First Year Engineering sem-2 Computer programing 2 (CP-2) programs.These are tried and tested and work just fine.Njoy!
Please refer this to all your pals on all your social networks..thank you !
The grey color is only for the background it should cause no problems.






-------------------------------------------------------------------------------------------------------------
/*Program to check if a word(string) is palindrome or not
INPUT:liril then Output would be : Palindrome: liril */
import java.io.*;
class Palin {
public static void main(String args[]) throws java.io.IOException
{
DataInputStream buf = new DataInputStream(System.in);
System.out.print("Enter a string: ");
String str = buf.readLine();
char x[] = str.toCharArray();
int i;
int j;
for (i = 0; i <= x.length/2; i++)
{
if (x[i] != x[x.length - 1 - i])
{
i = -1;
break;
}
}
if (i == -1)
System.out.println("Not a palindrome: " + str);
else
System.out.println("Palindrome: " + str);
}
}

/*Palindrom means a word which spells the same both frontwards and backwards
eg:malayalam
eg:liril
eg:mcbgbbgbcn
*/

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Followers

 

Mumbai University Engineering. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com