统计范围内的元音字符串数(函数)

统计范围内的元音字符串数(函数)

两种写法

第一种

int vowelStrings(char** words, int wordsSize, int left, int right) {

    int count = 0; // 初始化计数器

 

    // 遍历给定范围内的字符串数组

    for (int j = left; j <= right; j++) {

        int t = strlen(words[j]); // 获取当前字符串的长度

 

        // 检查当前字符串是否以元音字母开头

        if (words[j][0] == 'a' || words[j][0] == 'e' || words[j][0] == 'i' || words[j][0] == 'o' || words[j][0] == 'u') {

            // 如果以元音字母开头,再检查是否以元音字母结尾

            if (words[j][t - 1] == 'a' || words[j][t - 1] == 'e' || words[j][t - 1] == 'i' || words[j][t - 1] == 'o' || words[j][t - 1] == 'u') {

                count++; // 如果是以元音字母开头并以元音字母结尾的字符串,则计数器加1

            }

        }

    }

 

    return count; // 返回计数结果

}

第二种

int isVowel(char c) {

    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; // 判断字符是否为元音字母

}

 

int vowelStrings(char** words, int wordsSize, int left, int right) {

    int count = 0; // 初始化计数器

    for (int j = left; j <= right; j++) { // 遍历给定范围内的字符串数组

        int t = strlen(words[j]); // 获取当前字符串的长度

        if (t == 0) continue; // 如果字符串长度为0,则跳过当前循环

 

        if (t == 1) { // 如果字符串长度为1

            if (isVowel(words[j][0])) { // 检查字符是否为元音字母

                count++; // 如果是元音字母,则计数器加1

            }

        } else { // 如果字符串长度大于1

            if (isVowel(words[j][0]) && isVowel(words[j][t - 1])) { // 检查首尾字符是否均为元音字母

                count++; // 如果首尾字符均为元音字母,则计数器加1

            }

        }

    }

    return count; // 返回计数结果

}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值