Saturday, November 16, 2013

Java StringBuilder class

Write a program that prompts the user to enter a String and prints whether it is a paliandrome or not.Use StringBuilder class.


SOLUTION:

  
import java.util.Scanner;

public class Palindrome {
 public static void main(String args[]) {
  //Creating Scanner class
  Scanner input=new Scanner(System.in);
  //Reading string
  System.out.println("Enter the string :");
  String str;
  str=input.next();
  
  //Creating string builder object
  StringBuilder sf=new StringBuilder(str);
  sf.reverse();
  String a=sf.toString();

  if(str.toUpperCase().equals(a.toUpperCase())) {
   System.out.println("It's a Palindrome");
  }
  else {
   System.out.println("Not a Palindrome");
  }
 }
}

No comments:

Post a Comment