Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WordFlowDetector
- {
- private HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'y' };
- private HashSet<char> consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
- public bool IsWellFlowed(string word)
- {
- word = word.ToLower();
- List<string> syllables = SplitIntoSyllables(word);
- bool isVowelStart = false;
- foreach (string syllable in syllables)
- {
- bool isSyllableWellFlowed = IsSyllableWellFlowed(syllable, isVowelStart);
- if (!isSyllableWellFlowed)
- return false;
- isVowelStart = !isVowelStart;
- }
- return true;
- }
- private List<string> SplitIntoSyllables(string word)
- {
- List<string> syllables = new List<string>();
- string currentSyllable = "";
- for (int i = 0; i < word.Length; i++)
- {
- char currentChar = word[i];
- currentSyllable += currentChar;
- if (i == word.Length - 1 || IsVowel(currentChar) != IsVowel(word[i + 1]))
- {
- syllables.Add(currentSyllable);
- currentSyllable = "";
- }
- }
- return syllables;
- }
- private bool IsSyllableWellFlowed(string syllable, bool isVowelStart)
- {
- char startChar = isVowelStart ? 'v' : 'c';
- foreach (char c in syllable)
- {
- if ((IsVowel(c) && startChar != 'v') || (!IsVowel(c) && startChar != 'c'))
- return false;
- startChar = (startChar == 'v') ? 'c' : 'v';
- }
- return true;
- }
- private bool IsVowel(char c)
- {
- return vowels.Contains(c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement