Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Word
- {
- private String theWord; // the word converted to lowercase
- private String theWordOriginal; //the original word
- private static final String vowels = "aeiouy";
- Word(String w)
- {
- theWord = w.toLowerCase();
- theWordOriginal = w;
- }
- //-----------------------------------------------------------------
- // Things to note
- // 1. e endings ignored
- // 2. consecutive vowels count as one syllable
- // 3. words of length three or shorter count as a single syllable
- //-----------------------------------------------------------------
- public int countSyllables()
- {
- int syllables = 0;
- // check if word is of length 3
- if( theWord.length() <= 3 )
- {
- syllables = theWord.length() > 0;
- return syllables;
- }
- // prune the word to ignore 'e' at the end
- String prunedWord;
- if( theWord.endsWith("e") )
- prunedWord = theWord.substring(0, theWord.length()-2); // if last letter is an e, remove it and then do calculation
- else
- prunedWord = theWord; // else, we just use the string as is
- /*
- Now go thru each character
- if last character is not a vowel and current one is
- increment syllable count
- save current vowel state in lastIsVowel
- */
- boolean lastIsVowel;
- for(int i = 0, len = prunedWord.length(); i < len; i++)
- {
- boolean curIsVowel = isVowel( prunedWord.charAt(i) );
- if ( curIsVowel && !lastIsVowel ) syllables++;
- lastIsVowel = curIsVowel;
- }
- if( syllables == 0 ) syllables++;
- return syllables;
- }
- public String getWord()
- {
- return theWordOriginal;
- }
- static boolean isVowel(char ch)
- {
- return vowels.indexOf(ch) >= 0;
- }
- }
Add Comment
Please, Sign In to add comment