Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int countWords(String s){
- int counter = 0;
- boolean word = false;
- int endOfLine = s.length() - 1;
- for (int i = 0; i < s.length(); i++) {
- // if the char is letter, word = true.
- if (Character.isLetter(s.charAt(i)) == true && i != endOfLine) {
- word = true;
- // if char isnt letter and there have been letters before (word
- // == true), counter goes up.
- } else if (Character.isLetter(s.charAt(i)) == false && word == true) {
- counter++;
- word = false;
- // last word of String, if it doesnt end with nonLetter it
- // wouldnt count without this.
- } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
- counter++;
- }
- }
- return counter;
- }
- int CountWords (String in) {
- String[] words = in.split(" "); //separate string around spaces
- return words.length();
- }
Add Comment
Please, Sign In to add comment