Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

java - String matching program

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

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

1 Reply

0 votes
by (71.8m points)

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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...