0

How can I get my program to read the syllables of a string text, while recognizing two specific conditions:

  1. two vowels put together are one syllable.
  2. an 'E' at the end of a word is not a syllable.

My program so far,

int countS=0;
String scanSyll=text.toUpperCase();
for(int i=0;i<text.length();i++)
{
    char nSyllables=scanSyll.charAt(i);

    if(nSyllables=='A' || nSyllables=='E' || nSyllables=='I' || nSyllables=='O' || nSyllables=='U' || nSyllables=='Y')
        countS=countS+1;
} 

`

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49

2 Answers2

0

Instead of using a fully programatic way, I would look into regular expressions in order to achieve this goal, as it may provide better flexibility and an easier approach to find your matches.

The regex pattern could be something like such:

([bcdfghjklmnpqrstvwx]{1}[aeiouy]+)

This basically says that any single consonant followed by one or more vowels would trigger a match.

As a quick example you could use something like such:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
// ...
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("([bcdfghjklmnpqrstvwx]{1}[aeiouy]+)").matcher(yourStringHere);
while (m.find()) {
   allMatches.add(m.group());
}

Stackoverflow reference for matching all: Create array of regex matches

Nice Regex online tool: https://regex101.com/

For more information on regular expressions and how to use them, check out this page: http://www.regular-expressions.info/

Community
  • 1
  • 1
autronix
  • 2,885
  • 2
  • 22
  • 28
0
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class SyllableRecognizer
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> allMatches = new ArrayList<String>();
        Matcher m = Pattern.compile("([bcdfghjklmnpqrstvwx]{1}[aeiouy]{2,}[^$e])",Pattern.CASE_INSENSITIVE).matcher("aakaabboe");
        while (m.find())
        {
            allMatches.add(m.group());
        }

    System.out.print(allMatches.size());

    }
}
Mok
  • 277
  • 1
  • 6
  • 16