-5

How should I get the total no. of substrings in a string. For all substrings in a string.

Ex:

str="This is this my book is This"

O/p should like below:

This-3
Is=2
my=1
book=1
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 1
    Split using the blank character; iterate over the result list, match each result and count matches. If you face a concrete issue, come back here. Otherwise this seems to be a "please do my homework" question. – jp-jee Jul 13 '16 at 18:04
  • @jp-jee This is the second question I've seen in the last day or two asking this very specific question - how to count words in a string. They are probably in the same class. – Loduwijk Jul 13 '16 at 18:10
  • Possible duplicate of [count number of distinct words](http://stackoverflow.com/questions/6454348/count-number-of-distinct-words) – Michael Gaskill Jul 13 '16 at 18:57
  • @prakash If an user answered your question please also **accept** his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Jul 27 '17 at 12:25

3 Answers3

0

If I'm right you want to search for the occurrences of all words, not all possible substrings. A very small, easy to understand, code would be the following:

// Split at space
String[] words = input.split(" ");
HashMap<String, Integer> countingMap = new HashMap<>();
for (String word : words) {
    Integer counter = countingMap.get(word);
    if (counter == null)) {
        counter = 0;
    }
    countingMap.put(word, counter + 1);
}

However, this approach is limited as it assumes each word is surrounded by a space.

Regex is a more powerful tool, it provides a special character for a word boundary (this also matches ,.!? and so on). Consider the following Pattern:

\b(.+?)\b

You can see an example here: regex101.com/r/hO8kA0/1

How to do this in Java?

Pattern pattern = Pattern.compile("\\b(.+?)\\b");
Matcher matcher = pattern.matcher(input);

while(matcher.find()) {
    String word = matcher.group(1);
    // Here is your word, count the occurrences like above
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Given the OP hasn't even attempted this problem and it honestly looks like homework, I would not give a whole solution. http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – Adam Jul 13 '16 at 18:10
0

If I understood you correctly this is a solution for your problem:

    String str="This is this my book is This";
    Map<String, Integer> counts = new HashMap<String, Integer>();

    String[] words = str.toLowerCase().split("[\\s\\.,;!\\?]");

    for (String word: words) {
        int count = counts.containsKey(word) ? counts.get(word).intValue() : 0;
        counts.put(word, Integer.valueOf(count + 1));
    }

You just split the string by the delimiters you want to consider and collect the occurrences in a map.

akostajti
  • 2,997
  • 1
  • 14
  • 6
  • Given the OP hasn't even attempted this problem and it honestly looks like homework, I would not give a whole solution. http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – Adam Jul 13 '16 at 18:10
  • Or even better, give him a Java 8 Lambda solution. Goodluck trying to get credit on that. – Orin Jul 13 '16 at 18:11
0
String str="This is this my book is This";
String[] words = str.split(" ");
Map<String,Integer> unitwords = new HashMap<String,Integer>;
for(String word: words){
  if(unitwords.containsKey(word)){
    unitwords[word]++;
  }else{
    unitwords.add(word,1);
}

And print the map unitwords.