0

I am looking to create a method that counts the number of syllables within a word. A syllable defined in my project as a contiguous group of vowels but not an 'e' at the end of the word. So the word 'obvious' by this definition only has 2 syllables and the string 'obstinanceeeeeee' has 3 syllables. My attempt is:

protected int countSyllables(String word)
String input = word.toLowerCase();
    int i = input.length() - 1;
    int syllables = 0, numOfE = 0;
    // skip all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
        numOfE++;
    }

    // This counts the number of consonants within a word
    int j = 0;
    int consonants = 0;
    while (j < input.length()) {
        if (!isVowel(input.charAt(j))) {
            consonants++;
        }
        j++;
    }

    // This will return syllables = 1 if the string is all consonants except for 1 e at the end.
    if (consonants == input.length() - 1 && numOfE == 1) {
        syllables = 1;
    }

    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
    return syllables;
}

public boolean isVowel(char ch) {
   if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
       return true;
   }
   return false;
}
Don Lim
  • 21
  • 5
  • 4
    Hint: you want to check OR conditions with the `||` operator. – moonwave99 Dec 10 '15 at 18:31
  • 1
    Can you please add the current output of your program and the desired one? Consider adding a description about the difference. – Marcinek Dec 10 '15 at 18:39
  • Possible duplicate of [Java - Writing a syllable counter based on specifications](http://stackoverflow.com/questions/9154027/java-writing-a-syllable-counter-based-on-specifications) – Frakcool Dec 10 '15 at 18:43

1 Answers1

1
  protected int countSyllables(String word) {
    String input = word.toLowerCase();
    int i = input.length() - 1;
    // skip all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
    }
    int syllables = 0;
    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
        return syllables;
    }

    public boolean isVowel(char ch) {
       if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
           return true;
       }
       return false;
    }

I hope this helps. You can start iterating from the end of the string and ignore all the e's before processing/counting the syllables.
Making edit to the original answer. Now will count the word as syllable if it has only one e in the end

 protected int countSyllables(String word) {
    String input = word.toLowerCase();
    int syllables = 0,numOfEInTheEnd=0;

    int i = input.length() - 1;
    // count all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
        numOfEInTheEnd++;
    }

    if (numOfEInTheEnd == 1) {
        syllables = 1;
    }

    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
    return syllables;
    }

    public boolean isVowel(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
        return true;
    }
    return false;
    }

Instead of skipping all the trailing e's, now its counting it. If trailing e's is equal to 1, syllables is set to 1. This will work now.

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
  • Thanks for your quick reply. Your answer worked great however if there was a string such as 'be' or 'mmmmeeeee' or 'the', countSyllables would return 0 except it should be 1. I'm not sure how I would include this. – Don Lim Dec 11 '15 at 05:30
  • Can you please clarify this? According to the problem statement answer for 'obstinanceeeeeee' is 3 and why is it 1 for 'be'? How do you want to treat the 'e's in the end of the String or how do you differentiate it? is it that if there's a single 'e' in the end, it has to counted? – Ankit Deshpande Dec 11 '15 at 12:25
  • Yes. For words such as 'me', 'the', and 'be', while (i >= 0 && input.charAt(i) == 'e') { i--; } gets rid of the character 'e' but will return syllables = 0. – Don Lim Dec 11 '15 at 16:49
  • I have made corresponding changes. It will work now. Check the edited ans – Ankit Deshpande Dec 11 '15 at 17:41
  • Thank you again for your help. I added 2 sections of code that would return 1 syllable if there is a string such as 'sdlfjse' (any string with all consonants and an 'e' at the end) but not double count syllables for strings such as 'one'. – Don Lim Dec 11 '15 at 18:49