简体   繁体   English

查找字数”- 我的代码无法正常工作

[英]Find Word Count"- My code doesn't work properly

"Find Word Count"- Instructions: Given an input string (assume it's essentially a paragraph of text) and a word to find, return the number of times in the input string that the word is found. “查找字数”- 说明:给定一个输入字符串(假设它本质上是一段文本)和一个要查找的单词,返回在输入字符串中找到该单词的次数。 Should be case agnostic and remove space, commas, full stops, quotes, tabs etc while finding the matching word.在查找匹配的单词时,应该不区分大小写并删除空格、逗号、句号、引号、制表符等。

======================= ========================

My code doesn't work properly.我的代码无法正常工作。

`
String input = " It can hardly be a coincidence that no language on" +
                " Earth has ever produced the expression as pretty as an airport." +
                " Airports are ugly. Some are very ugly. Some attain a degree of ugliness" +
                " that can only be the result of a special effort. This ugliness arises " +
                "because airports are full of people who are tired, cross, and have just " +
                "discovered that their luggage has landed in Murmansk (Murmansk airport " +
                "is the only known exception to this otherwise infallible rule), and architects" +
                " have on the whole tried to reflect this in their designs. They have sought" +
                " to highlight the tiredness and crossness motif with brutal shapes and nerve" +
                " jangling colors, to make effortless the business of separating the traveller" +
                " for ever from his or her luggage or loved ones, to confuse the traveller with" +
                " arrows that appear to point at the windows, distant tie racks, or the current " +
                "position of Ursa Minor in the night sky, and wherever possible to expose the " +
                "plumbing on the grounds that it is functional, and conceal the location of the" +
                "departure gates, presumably on the grounds that they are not.";
        input = input.toLowerCase();
        String whichWord = "be";
        whichWord = whichWord.toLowerCase();
        int lastIndex = 0;
        int count = 0;

        while(lastIndex != -1){

            lastIndex = input.indexOf(whichWord,lastIndex);

            if(lastIndex != -1){
                count ++;
                lastIndex += whichWord.length();
            }
        }
        System.out.println(count);
`

In your code you are not checking complete word.在您的代码中,您没有检查完整的单词。 So, its matching both 'be' and 'because'.因此,它匹配“是”和“因为”。 You're checking if there are any sub-strings contains the word 'be'.您正在检查是否有任何子字符串包含单词“be”。 Could you please try below solution using regex?您能否使用正则表达式尝试以下解决方案? It will solve your purpose:它将解决您的目的:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordCount {

    public static void main(String[] args) {
        String input = " It can hardly be a coincidence that no language on" +
                " Earth has ever produced the expression as pretty as an airport." +
                " Airports are ugly. Some are very ugly. Some attain a degree of ugliness" +
                " that can only be the result of a special effort. This ugliness arises " +
                "because airports are full of people who are tired, cross, and have just " +
                "discovered that their luggage has landed in Murmansk (Murmansk airport " +
                "is the only known exception to this otherwise infallible rule), and architects" +
                " have on the whole tried to reflect this in their designs. They have sought" +
                " to highlight the tiredness and crossness motif with brutal shapes and nerve" +
                " jangling colors, to make effortless the business of separating the traveller" +
                " for ever from his or her luggage or loved ones, to confuse the traveller with" +
                " arrows that appear to point at the windows, distant tie racks, or the current " +
                "position of Ursa Minor in the night sky, and wherever possible to expose the " +
                "plumbing on the grounds that it is functional, and conceal the location of the" +
                "departure gates, presumably on the grounds that they are not.";

        input = input.toLowerCase();
        String whichWord = "be";
        whichWord = whichWord.toLowerCase();

        int count = 0;
        String regex = "(\\W|^)" + whichWord + "(\\W|$)";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        while(matcher.find()) {
            count++;
        }

        System.out.println(count);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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