Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Counts chars in each sentence of a given paragraph.
* Sentence end counts as '.', '!' or '?'.
*
* @return ArrayList of integers that correspond to the count of chars of each sentence in the
* paragraph.
*/
private String countCharsInSentences(String paragraph) {
ArrayList<Integer> charsBySentence = new ArrayList<>();
int currentCharCount = 0;
for (char currentCharacter : paragraph.toCharArray()) {
if (currentCharacter == '.' || currentCharacter == '!' || currentCharacter == '?') {
charsBySentence.add(currentCharCount);
currentCharCount = 0;
}
if (Character.isLetter(currentCharacter)) {
currentCharCount++;
}
}
int temporaryMaxCharCount = 0;
int sentenceWithMostCharsIndex = 0;
for (int index = 0; index <= charsBySentence.size(); index++) {
if (charsBySentence.get(index) > temporaryMaxCharCount) {
temporaryMaxCharCount = charsBySentence.get(index);
sentenceWithMostCharsIndex = index;
}
}
return "The sentence with most words is number: " + (sentenceWithMostCharsIndex + 1) +
"\nWith " + temporaryMaxCharCount + " words.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.