Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to make a java program to reverse the given string and each time iterate, compare with the reversed string then to print pass if matched else fail.

My program is:

package sss;

import java.util.Scanner;

public class ssi {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String original,reverse="";
        Scanner sc=new Scanner(System.in);
        int ascii11,ascii12,ascii13,ascii14;
        System.out.println("enter the string to be reversed");
        original=sc.next();
        int length=original.length();
        for(int i=length-1;i>=0;i--)
        {
            reverse=reverse+original.charAt(i);
        }
        System.out.println(reverse);
        //System.out.println(original);
        for(int j=0;j<original.length()-1;j++)
        {
            ascii11=original.charAt(j);
            ascii12=original.charAt(j+1);
            ascii13=reverse.charAt(j);
            ascii14=reverse.charAt(j+1);
            if(Math.abs(ascii11-ascii12) == Math.abs(ascii13-ascii14))
            {

                System.out.println("pass");

            }
            else 
            {

                System.out.println("fail");
            }   
        }

        // TODO Auto-generated method stub
        sc.close();
    }
}

here each time when the for loop iterates i am getting pass or fail for each pair of numbers but i want the o/p as to print only pass or fail ONCE.

can any one help me out please...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

Example Code:

import java.util.Scanner;

public class PalindromeChecker {

    public static void main(String[] args) {

        String original;        
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the string to be reversed: ");
        original = sc.next();

        int halfLength = original.length()/2;
        int lastIndex = original.length() - 1;

        int i;
        for(i = 0; i < halfLength; i++) {
            if(original.charAt(i) != original.charAt(lastIndex - i)) {
                System.out.println("Fail!");
                break;
            }
        }

        // Managed to match all characters
        if(i == halfLength) {
            System.out.println("Pass!");
        }

        sc.close();
    }
}

Input/Output:

Enter the string to be reversed: banana
Fail!
Enter the string to be reversed: RADAR
Pass!
Enter the string to be reversed: asdwwdsa
Pass!

So the idea is to:

  • Compare the first half of the string with the second half of the string
  • Compare the first character with the last character
  • Compare the 2nd character with the 2nd last character, and so on.
  • If any character does not match, print Fail!
  • If the loop finishes, it implies that the string is a palindrome (original == reversed), print Pass!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...