计算文件中音节数量的程序

时间:2019-02-12 23:51:17

标签: c arrays loops

在给定这些约束的情况下,我有一个函数应该计算文件中音节的数量:

1)每组相邻的元音(a,e,i,o,u,y)都算作一个音节(例如,“ real”中的“ ea”算作一个音节,但“ e。 “富豪”中的“ a”算作两个音节

2)单词末尾的“ e”不算作音节

3)即使先前的规则计数为零,每个单词也至少具有一个音节。

鉴于此,我创建了(尽管很简陋)函数来计算文件中音节的数量

我曾尝试以多种不同的方式创建此功能,但是这种方式对我来说最有意义,并且还为我提供了一个合理答案(不是真的,而是按照它所做的大事来做)对真实答案的估计。

int syllableCount(char **str)
{
    int i = 0;
    int q = 0;
    int syllableCounter = 0;

    for (i = 0; i < lineCount; i++)
    {
        for (q = 0; q <= strlen(str[i]); q++)
        {
            if (str[i][q] == 'A' || str[i][q] == 'a' ||
                str[i][q] == 'E' || str[i][q] == 'e' ||
                str[i][q] == 'I' || str[i][q] == 'i' ||
                str[i][q] == 'O' || str[i][q] == 'o' ||
                str[i][q] == 'U' || str[i][q] == 'u' ||
                str[i][q] == 'Y' || str[i][q] == 'y')
            {
                syllableCounter++;
            }
            if ((str[i][q] == 'E' && str[i][q + 1] == ' ') ||
                (str[i][q] == 'e' && str[i][q + 1] == ' ') ||
                (str[i][q] == 'E' && str[i][q + 1] == '\n') ||
                (str[i][q] == 'e' && str[i][q + 1] == '\n') ||
                (str[i][q] == 'E' && str[i][q + 1] == '.') ||
                (str[i][q] == 'e' && str[i][q + 1] == '.') ||
                (str[i][q] == 'E' && str[i][q + 1] == ';') ||
                (str[i][q] == 'e' && str[i][q + 1] == ';') ||
                (str[i][q] == 'E' && str[i][q + 1] == ':') ||
                (str[i][q] == 'e' && str[i][q + 1] == ':') ||
                (str[i][q] == 'E' && str[i][q + 1] == '!') ||
                (str[i][q] == 'e' && str[i][q + 1] == '!') ||
                (str[i][q] == 'E' && str[i][q + 1] == '?') ||
                (str[i][q] == 'e' && str[i][q + 1] == '?'))
            {
                syllableCounter--;
            }
            if ((str[i][q] == 'A' || str[i][q] == 'a' ||
                 str[i][q] == 'E' || str[i][q] == 'e' ||
                 str[i][q] == 'I' || str[i][q] == 'i' ||
                 str[i][q] == 'O' || str[i][q] == 'o' ||
                 str[i][q] == 'U' || str[i][q] == 'u' ||
                 str[i][q] == 'Y' || str[i][q] == 'y') &&
                (str[i][q + 1] == 'A' || str[i][q + 1] == 'a' ||
                 str[i][q + 1] == 'E' || str[i][q + 1] == 'e' ||
                 str[i][q + 1] == 'I' || str[i][q + 1] == 'i' ||
                 str[i][q + 1] == 'O' || str[i][q + 1] == 'o' ||
                 str[i][q + 1] == 'U' || str[i][q + 1] == 'u' ||
                 str[i][q + 1] == 'Y' || str[i][q + 1] == 'y'))
            {
                syllableCounter--;
            }
            if ((str[i][q] != 'A' || str[i][q] != 'a' ||
                 str[i][q] != 'E' || str[i][q] != 'e' ||
                 str[i][q] != 'I' || str[i][q] != 'i' ||
                 str[i][q] != 'O' || str[i][q] != 'o' ||
                 str[i][q] != 'U' || str[i][q] != 'u' ||
                 str[i][q] != 'Y' || str[i][q] != 'y') &&
                (str[i][q + 1] == ' ' || str[i][q + 1] == '\n'))
            {
                syllableCounter++;
            }
        }
    }
    return syllableCounter;
}

在我的测试文件中,我有54个音节,真正的答案是32。是什么让我差22分!?

P.S:这是我使用的文件中的文本:

“红色现成的裙子是为您量身定制的!它 明天要准备好。什么是 衣服的颜色?哦,是红色的!”

所有拼写和空格错误都是故意的

1 个答案:

答案 0 :(得分:3)

您的代码太复杂了。

原始问题陈述中的规则非常简单。对于每个单词,您需要计算遇到多少个不同的元音组。使用状态机的注释中的建议是绝对正确的。但是,您不需要复杂的机器。您只需要跟踪一些基本状态即可。

我至少建议以下状态:

int in_word = 0;         // non-zero if currently processing a word
int in_vowels = 0;       // non-zero if currently processing group of vowels
int is_silent_e = 0;     // non-zero if the last vowel processed was an 'e'
int vowel_groups = 0;    // counts the number of vowel groups encountered in current word

现在,具有上述明智的状态,以下是如何使用它们的概述:

for (char *p = str[i], *end = p + strlen(str[i]) + 1; p != end; ++p)
{
    char c = tolower(*p);
    if (isalpha(c))
    {
        // starting a new word?
        if (!in_word) {
            in_word = 1;
            in_vowels = 0;
            is_silent_e = 0;
            vowel_groups = 0;
        }

        // do we have a vowel?
        if (strchr("aeiouy", c)) {
            if (!in_vowels) {
                /**** WRITE ME ****/
                ++vowel_groups;
            } else {
                /**** WRITE ME ****/
            }
        } else if (in_vowels) {
            // no longer in vowel group
            /**** WRITE ME ****/
        }
    }
    else if (in_word)
    {
        // No longer in a word -- update syllable count and reset
        vowel_groups -= is_silent_e;

        /**** WRITE ME ****/
    }
}

我已经为您填写了一些逻辑。正确执行此操作后,您将获得示例输入的答案32

请注意特殊的循环条件,以确保循环也处理字符串的null终止符。这样即使在字符串中的最后一个字符是单词字符的情况下,也可以确保结束单词测试。