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?
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?
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 .
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); 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++; } String operator == . Do not equals ? - FlippyString comparison in == ? - Vadim ProkopchukSource: https://ru.stackoverflow.com/questions/616930/
All Articles