如何检查句子中的回文数量?

时间:2016-12-04 09:37:54

标签: java string count palindrome

我是新来的。我会感激一些帮助,我正在尝试编写一个程序来查找句子中的回文,这是我到目前为止所做的。但是,每当我执行它时,回文数量变为零,并且for和ifs中的System.out.println不会发生,我无法理解什么是错误的。我们必须使用for循环,而不是while。

import java.util.Scanner;

class palcheck {
    public void main() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence please...");// input sentece
        String s = sc.next();// the sentence

        String wordback = ""; //reverse of a word of the sentence
        int count = 0; // palindrome count
        String word = "";// for a word of the sentence
        int a; // counter
        String s2 = null; //sentence but with removed full stop

        if (s.charAt(s.length() - 1) == '.')// checking if ends with fullstop

        {
            s2 = s.substring(0, (s.length() - 1));
        }// sentence without fullstop
        System.out.println(s2);
        String s3 = " " + s2 + " ";
        System.out.println(s3);// added a space;
        for (int c = 0; c < s3.length(); c++) {
            char isspace = s3.charAt(c);
            if (isspace == ' ')// checking
            {
                char x = ' ';// initilaizing
                for (a = s3.indexOf(isspace); x != ' '; a++) {
                    x = s3.charAt(a); //collecting letters for word
                    word = word + x; // forming word
                    System.out.println("word is " + word);// the formed word
                }
                int l2 = word.length();
                for (int i = l2 - 1; i >= 0; i--) {
                    wordback = wordback + word.charAt(i);// forming reverse word
                    System.out.println("reverse word is " + wordback);
                    if (word.equalsIgnoreCase(wordback)) {
                        count = count + 1;
                    }// increasing palindrome count
                    word = null;// resetting value
                    wordback = null;//resetting value
                }
            }
        }
        System.out.println("the no of palindromes is " + count);
    }
}

编辑:我试图更改变量的名称:

import java.util.Scanner;

class palcheck {
    public void main() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence please...");// input sentece
        String sentence = sc.next();// the sentence

        String wordreverse = ""; //reverse of a word of the sentence
        int count = 0; // palindrome count
        String word = "";// for a word of the sentence
        int a; // counter
        String sentencewithoutfullstop = null; //sentence but with removed full stop

        if (sentence.charAt(sentence.length() - 1) == '.')// checking if ends with fullstop

        {
            sentencewithoutfullstop = sentence.substring(0, (sentence.length() - 1));
        }// sentence without fullstop
        System.out.println(sentencewithoutfullstop);
        String sentencewithspace = " " + sentencewithoutfullstop + " ";
        System.out.println(sentencewithspace);// added a space;
        for (int c = 0; c < sentencewithspace.length(); c++) {
            char isspace = sentencewithspace.charAt(c);
            if (isspace == ' ')// checking
            {
                char letter = ' ';// initilaizing
                for (a = sentencewithspace.indexOf(isspace); letter != ' '; a++) {
                    letter = sentencewithspace.charAt(a); //collecting letters for word
                    word = word + letter; // forming word
                    System.out.println("word is " + word);// the formed word
                }
                int length2 = word.length();
                for (int i = length2 - 1; i >= 0; i--) {
                    wordreverse = wordreverse + word.charAt(i);// forming reverse word
                    System.out.println("reverse word is " + wordreverse);
                    if (word.equalsIgnoreCase(wordreverse)) {
                        count = count + 1;
                    }// increasing palindrome count
                    word = null;// resetting value
                    wordreverse = null;//resetting value
                }
            }
        }
        System.out.println("the no of palindromes is " + count);
    }
}

编辑:我重做了程序,但没有执行。我正在使用bluej,每当我选择void(main)时,java虚拟机一直在运行,我必须最终终止它。

程序中有问题吗?

import java.util.Scanner;

class cal {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        String word = null;
        String reverse = null;
        int count = 0;
        if (input.endsWith(".")) {
            String Sent2 = input.substring(0, (input.length() - 1));
            String sent3 = " " + Sent2 + " ";
            int l = sent3.length();
            for (int i = 0; i < l; i++) {
                if (sent3.charAt(i) == ' ') {
                    while (sent3.charAt(i + 1) != ' ') {
                        char h = sent3.charAt(i + 1);
                        word = word + h;
                        int l2 = word.length();
                        for (int j = l2 - 1; j >= 0; j--) {
                            char hg = word.charAt(j);
                            reverse = reverse + hg;
                            if (word.equalsIgnoreCase(reverse)) {
                                System.out.println("palindrome :" + word);
                                count++;
                            }
                        }
                    }
                }
            }
        } else {
            String sent3 = " " + input + " ";
            int l = sent3.length();
            for (int i = 0; i < l; i++) {
                if (sent3.charAt(i) == ' ') {
                    while (sent3.charAt(i + 1) != ' ') {
                        char h = sent3.charAt(i + 1);
                        word = word + h;
                        int l2 = word.length();
                        for (int j = l2 - 1; j >= 0; j--) {
                            char hg = word.charAt(j);
                            reverse = reverse + hg;
                            if (word.equalsIgnoreCase(reverse)) {
                                System.out.println("palindrome :" + word);
                                count++;
                            }
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请避免代码中的Systax错误:

在Java中,main函数必须具有以下内容

public static void main(String args[])
{

并且对于循环你提到的条件永远不会满足。因为在循环之前你初始化了像

char letter = ' ' ;// initilaizing
 for( a = sentencewithspace.indexOf(isspace);  letter !=' ' ;a++)

一次,循环条件满足,然后只在内部块执行。

请不要在循环中使用indexOf()。 当String索引超出范围时,原因是java throw StringIndexOutOfBoundsException

参考链接:http://www.vinaysingh.info/palindromic-words/

how to count the number of words of the same(PALINDROME) in java

答案 1 :(得分:0)

此代码假设您有input String并在其中找到回文。

String word = "";
String inverse = "";
for (int index = 0; index < input.length(); index++) {
    if (input.charAt(index) == " ") {
        if ((word.length() > 0) && (word.equals(inverse))) {
            System.out.println("Palindrome: " + word);
        }
        word = "";
        inverse = "";
    } else {
        char current = input.charAt(index);
        word += current;
        inverse = current + inverse;
    }
}
if ((word.length() > 0) && (word.equals(inverse))) {
    System.out.println("Palindrome: " + word);
}