public class longestsentence {
public static void main (String[] args){
longestsentence test = new longestsentence();
System.out.println(test.solution("We test coders. Give us a try?")); //4
System.out.println(test.solution("Forget CVs..Save time . x x")); //2
System.out.println(test.solution(".")); //0
System.out.println(test.solution("We test coders give us a try")); //7
}
public int solution (String s){
String[] sentencesplits = s.trim().split("[.?!]");
int maxwords = 0;
for (int i = 0; i < sentencesplits.length; i++){
String[] wordssplits = sentencesplits[i].trim().replaceAll(" +", ".").split("[.]");
if (maxwords < wordssplits.length){
maxwords = wordssplits.length;
}
}
return maxwords;
}
}
No comments:
Post a Comment