Advertisement
Guest User

countSyllable

a guest
Apr 16th, 2017
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1.  
  2. private int countSyllables(String word)
  3. {
  4. int count = 0;
  5. String lcWord = word.toLowerCase();
  6.  
  7. char[] wordArr= lcWord.toCharArray() ;
  8. for(int i = 0;i<wordArr.length-1;i++) {
  9.  
  10. if(isVowel(wordArr[i])) {
  11. count++;
  12. if(i > 0) {
  13. if(isPreVowel(wordArr, i)) {
  14. count--;
  15. }
  16. }
  17. }
  18. }
  19. //System.out.println(count);
  20. if(lcWord.charAt(wordArr.length-1)=='e' && count > 0) {
  21. count ++ ;
  22. }
  23. if(lcWord.charAt(wordArr.length-1) == 'a'||
  24. lcWord.charAt(wordArr.length-1) == 'i'||
  25. lcWord.charAt(wordArr.length-1) == 'o'||lcWord.charAt(wordArr.length-1) == 'u'||lcWord.charAt(wordArr.length-1)
  26. == 'y'){
  27. count++;
  28. }
  29. if(isPreVowel(wordArr,wordArr.length-1)){
  30. count--;
  31. }
  32.  
  33. //System.out.println(count + lcWord);
  34.  
  35. return count;
  36. }
  37. private static boolean isPreVowel(char[] wordArr,int i) {
  38. if(isVowel(wordArr[i-1])) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. private static boolean isPostVowel(char[] wordArr,int i) {
  44. if(isVowel(wordArr[i+1])) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. private static boolean isVowel(char c) {
  50. if(c == 'a'||c == 'e'||c == 'i'||c == 'o'||c == 'u'||c == 'y') {
  51. return true;
  52. }
  53. return false;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement