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

拜拜、爱过 提交于 2020-01-22 02:09:08

问题


I'm a newbie Java Developer. I want to write code to count the number of palindrome words in the paragraph using Java.
The assumptions are : User can enter a paragraph containing as many sentences as possible. Each word is separated by a whitespace, and each sentence is separated by a period and The punctuation right before or after the word will be ignored, while the punctuation inside the word will be counted.
Sample Input : Otto goes to school. Otto sees a lot of animals at the pets store.
Sample output : Otto = 2 a = 1 Sees = 1


回答1:


Read the file into your program, split the entries at every space and enter those into an arraylist. Afterwards, apply your palindrome algorithm onto each value in your arraylist and keep track of the words that were a palindrome and their occurences (for example a 2D array, or an arraylist with an object that holds both values).

When you've followed these steps, you should pretty much be there. More specific help will probably be given once you've shown attempts of your own.




回答2:


Using Collections in java will reduce the programming effort

Algorithm :

  1. Read the paragraph to a String variable

  2. Split the String using StringTokenizer using token as ' '(space) and add each word to ArrayList (Set wont allow duplicates)

  3. Write a method which return boolean (TRUE/ FALSE) value based on whether a given String is palindrome or not.

  4. Define a Map to hold the values of palindrome String and number of times it is repeated.

  5. If yes add the String to Map with key as palindrome String and value as number of times else dont add the String to Map

  6. Repeat the same logic until all the words are finished

Sample Code:

` public class StringPalindromeCalculator {

   private Map<String, int> wordsMap = new HashMap<>();
   private List<String> wordsList = new ArrayLiat<>();

   private boolean isPalindrome(String inputString) {
      // write String palindrome logic here
   }

   public Map<String, int> findPalindromeWords(String completeString) {
       StringTokenizer wordTokenizer = new StringTokenizer(completeString, ' ');
       while(wordTokenizer.hasMoreTokens()) {
            wordsList.add(wordTokenizer.nextToken());
       }
       for(String word : wordsList) {
            if(isPalindrome(word)) {
                 if(wordsMap.containsKey(word)) {
                      // increment the value of word
                 }
            } else {
                    // put the word into Map and return the map value 
            }

       }
       return wordsMap;
   }  

}`

Hope this Helps :)




回答3:


public class Palindrome {

int count = 0;

public static void main(String[] args) {
    String a = "malayalammadyoydaraarasdasdkfjasdsjhtj";
    Palindrome palindrome = new Palindrome();
    palindrome.countPalin(a);
}

private int countPalin(String str) {
    for (int i = 0; i < str.length() - 1; i++) {
        char start = str.charAt(i);
        String st = "";
        st += start;
        for (int j = i + 1; j < str.length(); j++) {

            st += str.charAt(j);

            StringBuffer rev = new StringBuffer(st).reverse();
            if (st.equals(rev.toString()) && st.length() > 1) {
                System.out.println(st.toString());
                count++;
            }
        }

        st = "";
    }

    System.out.println("Total Count : " + count);

    return count;
}

}



来源:https://stackoverflow.com/questions/13977129/how-to-count-the-number-of-words-of-the-samepalindrome-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!