回文编码

时间:2013-04-04 16:37:29

标签: java palindrome

我正在尝试使用方法在java中创建一个Palindrome测试器..这是我到目前为止所拥有的。它是如此接近我只是无法弄清楚为什么它不会说它是一个回文并扭转它。

System.out.println("Fun with Palindromes!!");

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the potential palindrome (or enter exit to quit): ");
    String x = in.nextLine();

    while(!x.equals("exit"))
    {
        String t = x.toLowerCase();
        String u = CleanUpString(t);
        Boolean wordCheck = checkPalindrome(u);
        int wordCount = x.length();

        String rev = "";

        for(int i = 0; i <x.length(); i++)
        {
            rev = x.charAt(i)+rev;
        }
        if(wordCheck == true)
        {
            System.out.println("The orginal string\"" + u + "\" contains" + wordCount + "characters." );
            System.out.println("The converted string\"" + rev + "\"is a palindrome");
        }
        else if(wordCheck == false)
        {
            System.out.println("The string \"" + u + "\" contains " + wordCount + " characters");
            System.out.println("\"" + rev + "\" is not a palindrome");
        }
    System.out.println("\nEnter the potential palindrome, or enter exit to quit: ");
    x = in.nextLine();

    }
}   
    public static String CleanUpString(String words)
    {
        words = words.replace(".","");
        words = words.replace("," ,"");
        words = words.replace(":","");
        words = words.replace("!","");
        return words;


    }   



    public static boolean checkPalindrome(String baseball)
    {
        String rev = "";
        for(int i = 0; i<baseball.length()-1; i++)
            {
                rev = baseball.charAt(i) + rev;

            }
        if(rev.equals(baseball))
            return true;
        else
            return false;

    }

}

6 个答案:

答案 0 :(得分:1)

以下是我用来确定字符串是否为Palindrome字符串的代码:

private static boolean checkPalindrome(String str){
    if (str == null) 
        return false;
    int len  = str.length();
    for (int i=0;i<len/2 ; i++){
        if (str.charAt(i) != str.charAt(len - i - 1)){
            return false;
            }
    }       
    return true;
}

对于反转字符串,您只需使用:

String reverse = new StringBuffer(string).reverse().toString();

希望这些可以帮到你。

答案 1 :(得分:1)

StringUtils用于此

import org.apache.commons.lang.StringUtils;

String isPalindrome(String word) {
    return StringUtils.reverse(word).equals(word);
}

答案 2 :(得分:0)

这是另一个选项

public class PalindromeTester {
    public static void main(String[] args) {

try {

    String s = args[0];
    int i = args[0].length()-1;
    int i2 = args[0].length();
    char [] chrs = new char[i2];

    for ( int i3 = i; i3 > -1; i3-- ) {
        chrs[i2-i3-1] = (s.charAt(i3) );
        }

        String s2 = String.valueOf(chrs);

        if ( s2.equals(s) ) {
            System.out.println( s + " is a palindrome!");
        } else { 
            System.out.println( s + " is not a palindrome");
        }

    } catch ( ArrayIndexOutOfBoundsException e ) {
        System.out.println("Please enter at least one letter or digit!");
    }

}
    }

答案 3 :(得分:0)

以下是我的表现:

public class palindromeTWO 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int right = 0;
        int left = 1;

        System.out.println("Please enter a word: ");
        String word = scan.next();

        int word_length = word.length();

        while(word.charAt(right) == word.charAt(word_length - left) && left < (word_length / 2))
        {
            left++;
            right++;
        }

        if(word.charAt(right) == word.charAt(word_length - left))
        {
            System.out.println("'" + word + "'" + " is a palindrome!");
        }
        else
            {
                System.out.println("'" + word + "'" + " is NOT a palindrome.");
            }
    }
}

答案 4 :(得分:0)

使用递归的第一个实现 -

import java.util.ArrayList;
import java.util.stream.Collectors;

public class PalindromeManager {
private static String str = "ehcache";
private static ArrayList<String> list = new ArrayList<>();

public static void main(String[] args) {
    test(str);

    String output = list.stream().collect(Collectors.joining());

    System.out.println(output);

    if (output.equals(str)) {
        System.out.println("it was palindrome");
    } else {
        System.out.println("Nope! it wasn't");
    }
}

private static void test(String str) {
    if (str.length() <= 0) {
        return;
    }

    String lastChar = "" + str.charAt(str.length() - 1);
    list.add(lastChar);
    test(str.substring(0, str.length() - 1));

  }
}

使用迭代的第二次实现 -

public class PalindromeManager2 {
private static String str = "ehcache";

public static void main(String[] args) {

    int startIndex = 0;
    int lastIndex = str.length() - 1;

    boolean result = true;

    while (true) {

        if (startIndex >= lastIndex) {
            break;
        }

        char first = str.charAt(startIndex);
        char last = str.charAt(lastIndex);

        /*if (first == ' ') {
            startIndex++;
            continue;
        }

        if (last == ' ') {
            lastIndex--;
            continue;
        }*/

        if (first != last) {
            result = false;
            break;
        }

        startIndex++;
        lastIndex--;

    }

    if (result) {
        System.out.println("Yes! It was");
    } else {
        System.out.println("Nope! it wasn't");
    }
  }
}

答案 5 :(得分:0)

在checkPalindrome方法中,将for循环的条件从i<baseball.length()-1更改为i<baseball.length()