Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Palindrome{
- Scanner sc = new Scanner(System.in);
- String word;
- String Word;
- int size;
- int i;
- int left;
- int right;
- char charAt;
- void initialize(){
- System.out.println("Give a word of which you want to know wether it is a palindrome or not.");
- word = sc.next();
- size = word.length();
- System.out.print(word+" - ");
- }
- boolean recursive(String Word){
- word = Word
- return palindrome(word, 0);
- }
- boolean palindrome( int i){
- if (i == size){
- System.out.print("palindrome - ");
- return true;
- } else if (word.charAt(size - i) == word.charAt(i)) {
- return palindrome(word, ++i);
- } else {
- System.out.print("no palindrome - ");
- return false;
- }
- }
- boolean iterative(String Word){
- word = Word
- left = 0;
- right = size;
- while(left < right){
- if (word.charAt(left) != word.charAt(right)){
- System.out.print("no palindrome");
- left++ ;
- right-- ;
- return false;
- }else{
- System.out.print("palindrome");
- return true;
- }
- }
- }
- }
- void show(){
- initialize();
- boolean recursive();
- boolean iterative();
- }
- public static void main(String[] args) {
- new Palindrome().show();
- }
Add Comment
Please, Sign In to add comment