There is a task:

A string containing English text is given.
Find the number of words starting with the letter b.

How can it be implemented?

Closed due to the fact that the essence of the question is not clear to the participants of iksuy , pavel , Igor , Kromster , VAndrJ Jan 19, '17 at 19:56 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    3 answers 3

    Option using a deterministic state machine:

    public static void main(String[] args) { String text = "banana ttt bbb b qwe"; int count = 0; int state = 1; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c == ' ') { state = 1; } else if (c == 'b' && state == 1) { count++; state = 0; } else { state = 0; } } System.out.println(count); } 

      Here is your solution:

       String text = "hello world banana bank"; String prefix = " b"; int count = 0; int currentIndex = 0; while (currentIndex < text.length() && (currentIndex = (text.indexOf(prefix, currentIndex) + 1)) != 0) count++; if(text.startWith(prefix.trim())) count++; System.out.println(count); 
      • You understand that the beginner will not understand with such if th? Of course, you understand the logic and it seems trivial, after the decision was born in my head. Only here the process goes the other way and the logic is understood at the moment of reading the code. - I. Perevoz
      • It will not break, the line contains a space prefix = "b" - Artem Konovalov
      • The task is trivial, and if the author tried, he would be able to cope without us. I propose such a "difficult" solution only because I myself wanted to solve it in a more optimal way. - Artem Konovalov
      • @Regent thanks, not carefully, read. Yes you are right. - Artem Konovalov

      Let there be a string containing the text

       string s = "какой-то текст"; 

      Then we use the split method for splitting a string, having previously added a space to the string, to take the last point into account for the punctuation mark.

       s += " "; string[] words = s.split("\\p{P}?[ \\t\\n\\r]+"); 

      Then we cycle through the array and look at the first letter.

       int count = 0; foreach (string word : words){ if (word[0] == 'b' || word[0] == 'B') count++; } 
      • creating an array, ihmo not very optimal. Although it is possible to search for matches and consider - Artem Konovalov
      • one
        I do not argue, your decision is better. I just showed Lily one of the options - Vadim Prokopchuk
      • I am plagued by vague doubts: either you did not write the code in Java , or you did not check it. Very similar to C # . - Regent
      • Is it possible to check the type of the String operator == . Do not equals ? - Flippy
      • @ SergeyGrushin and where is the String comparison in == ? - Vadim Prokopchuk