Last active
November 25, 2016 13:46
-
-
Save zurche/be5c23a700a1e3022da48dba3e79ff2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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