Friday, January 21, 2011

Palindrome


import java.io.*;

class Palindrome {

    void pali(String s) {
        int a = s.length();
        int mid = (a / 2);
        int count = 0;

        if (a % 2 == 0) {
            f:
            for (int i = 0, j = a - 1; i < mid || j >= mid; i++, j--) {
                if ((s.charAt(i)) == (s.charAt(j))) {
                    count++;
                    continue f;
                } else {
                    break f;
                }
            }
            if (count == mid) {
                System.out.println("word palindrome");
            } else {
                System.out.println("word no palindrome");
            }
        } else {
            j:
            for (int i = 0, j = a - 1; i < mid || j > mid; i++, j--) {
                if (s.charAt(i) == s.charAt(j)) {
                    count++;
                    continue j;
                } else {
                    break j;
                }
            }
            if (count == mid) {
                System.out.println("word palindrome");
            } else {
                System.out.println("word no palndrome");
            }
        }
    }
}

/**
 *
 * @author Sanket
 */
public class Main {
        public static void main(String[] args) throws IOException {
        Palindrome p = new Palindrome();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Word : ");
        String str = br.readLine();
        p.pali(str);
    }
}

No comments:

Post a Comment