Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int countSyllables(String word)
- {
- int count = 0;
- String lcWord = word.toLowerCase();
- char[] wordArr= lcWord.toCharArray() ;
- for(int i = 0;i<wordArr.length-1;i++) {
- if(isVowel(wordArr[i])) {
- count++;
- if(i > 0) {
- if(isPreVowel(wordArr, i)) {
- count--;
- }
- }
- }
- }
- //System.out.println(count);
- if(lcWord.charAt(wordArr.length-1)=='e' && count > 0) {
- count ++ ;
- }
- if(lcWord.charAt(wordArr.length-1) == 'a'||
- lcWord.charAt(wordArr.length-1) == 'i'||
- lcWord.charAt(wordArr.length-1) == 'o'||lcWord.charAt(wordArr.length-1) == 'u'||lcWord.charAt(wordArr.length-1)
- == 'y'){
- count++;
- }
- if(isPreVowel(wordArr,wordArr.length-1)){
- count--;
- }
- //System.out.println(count + lcWord);
- return count;
- }
- private static boolean isPreVowel(char[] wordArr,int i) {
- if(isVowel(wordArr[i-1])) {
- return true;
- }
- return false;
- }
- private static boolean isPostVowel(char[] wordArr,int i) {
- if(isVowel(wordArr[i+1])) {
- return true;
- }
- return false;
- }
- private static boolean isVowel(char c) {
- if(c == 'a'||c == 'e'||c == 'i'||c == 'o'||c == 'u'||c == 'y') {
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement