题目66 取最长元音字符串(ok)

定义当一个字符串只有元音字母(a,e,i,o,u,A,E,I,O,U)组成,
称为元音字符串,现给定一个字符串,请找出其中最长的元音字符串,
并返回其长度,如果找不到请返回0,
字符串中任意一个连续字符组成的子序列称为该字符串的子串

输入描述:
  一个字符串其长度 0<length ,字符串仅由字符a-z或A-Z组成
输出描述:
  一个整数,表示最长的元音字符子串的长度

示例1:
  输入
    asdbuiodevauufgh
  输出
    3
  说明:
    最长的元音字符子串为uio和auu长度都为3,因此输出3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_LEN 1000

// a,e,i,o,u,A,E,I,O,U
int IsYuanWord(char s)
{
    int result;

    switch (s) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            result = 1;
            break;
        default:
            result = 0;
            break;
    }

    return result;
}

int GetLen(char *s, int idx)
{
    int i;
    int count = 0;

    for (i = idx; i < strlen(s); i++) {
        if (IsYuanWord(s[i]) == 0) {
            break;
        }
        count++;
    }

    return count;
}

void GetYuanStr(char *s)
{
    int len = strlen(s);
    int i, pos = 0;
    int max = 0;
    int count;

    for (i = 0; i < len; i++) {
        if (IsYuanWord(s[i]) == 1) {
            count = GetLen(s, i);
            if (max < count) {
                max = count;
            }
            pos = i;
            i = i + count;
        }
    }

    printf("max=%d", max, pos);
}

int main()
{
    char s[MAX_LEN] = {0};

    gets(s);
    GetYuanStr(s);
    return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羊族的希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值