简体   繁体   中英

Check string if Symmetrical and palindrome

I just go the Symmetrical at makeuseof and it's currently in javascript and i converted it to Java, However there's an error on the line 38 which is the array. Please check the code below, Thank you.

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();
        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        var length = str.length();

        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            var pointer1 = 0;
            var pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str[pointer1] == str[pointer2]) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

Error: 在此处输入图像描述

Thanks to for sharing the String::charAt this is very helpful

I used charAt and convert double to int.

import java.util.Scanner;
    class check
    {
    public static void main(String args[])
    {
        String str, rev = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
        int length = str.length();

        for ( int i = length - 1; i >= 0; i-- )
            rev = rev + str.charAt(i);
        //Check if Symmetrical
        if (isSymmetrical(str)) {
            System.out.println(str +" is a symmetrical");
        } else {
            System.out.println(str +" is not a symmetrical");
        }
        //Check if palindrome
        if (str.equals(rev))
            System.out.println(str +" is a palindrome");
        else
            System.out.println(str +" is not a palindrome");
    }
    public static boolean isSymmetrical(String str){
        double midIndex;
        int length = str.length();
        
        if (length % 2 == 0) {
            midIndex = Math.floor(length/2);
        }
        else {
            midIndex = Math.floor(length/2) + 1;
        }
            int pointer1 = 0;
            double pointer2 = midIndex;
            while(pointer1 < midIndex && pointer2 < length) {
                if(str.charAt(pointer1) == str.charAt((int)pointer2)) {
                    pointer1 += 1;
                    pointer2 += 1;
                }
                else {
                    return false;
                }
            }
            return true;
    }
}

Output: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM