鱼C论坛

 找回密码
 立即注册
查看: 2524|回复: 5

[已解决]分类统计字符串中元音字母和其他字符的个数

[复制链接]
发表于 2018-1-17 14:32:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<conio.h>
#define N 100
void fun(char *str,int bb[])
{
        char *p=str;
        int i=0;
        for(i=0;i<6;i++)
                bb[i]=0;
        while(*p)
        {
                switch(*p)
                {
                case 'A':
                case 'a':bb[0]++;break;
                case 'E':
                case 'e':bb[1]++;break;
                case 'I':
                case 'i':bb[2]++;break;
                case 'O':
                case 'o':bb[3]++;break;
                case 'U':
                case 'u':bb[4]++;break;
                default:bb[5]++;

                }
                p++;
        }
}
main()
{
        char str[N],ss[5]="AEIOU";
        int i;
        int bb[6];
        printf("Input a string :\n");
        gets(str);
        printf("the string is :\n");
        puts(str);
        fun(str,bb);
        for(i=0;i<5;i++)
                printf("\n%c:%d",ss[i],bb[i]);
        printf("\n other:%d",bb[i]);
}
最佳答案
2018-1-23 15:28:21
ss[5]是字符串,中间的个数至少要比你输入的字符数多一个,因为字符串会自动在结尾处添加‘\0’,下面是对你的程序稍微修改了一下,并附上了运行结果
#include<stdio.h>
#include<conio.h>
#define N 100
void fun( char *str, int bb[])
{
        char *p = str;
        int i = 0;
        for( i = 0;i < 6; i++ )
                bb[i] = 0;
        while( *p )
        {
                switch( *p )
                {
                case 'A':
                case 'a':bb[0]++;break;
                case 'E':
                case 'e':bb[1]++;break;
                case 'I':
                case 'i':bb[2]++;break;
                case 'O':
                case 'o':bb[3]++;break;
                case 'U':
                case 'u':bb[4]++;break;
                default:bb[5]++;
                }
                p++;
        }
}
int main(void)
{
        char str[N],ss[6]="AEIOU";
        int i;
        int bb[6];
        printf("Input a string :\n");
        gets(str);
        printf("the string is :\n");
        puts(str);
        fun( str, bb );
        for(i = 0; i < 5; i++ )
                printf("\n%c:%d", ss[i], bb[i] );
        printf("\n other:%d \n",bb[i]);
                return 0;
}

/************************************

-------------------------------------
运行结果:
Input a string :
aeiouAEIOU123456789
the string is :
aeiouAEIOU123456789

A:2
E:2
I:2
O:2
U:2
other:9
请按任意键继续. . .


--------------------------------------
**************************************/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-1-17 14:33:11 | 显示全部楼层
--------------------Configuration: test34 - Win32 Debug--------------------
Compiling...
test34.cpp
F:\cproject\test34\test34.cpp(32) : error C2117: 'AEIOU' : array bounds overflow
F:\cproject\test34\test34.cpp(43) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

test34.exe - 1 error(s), 0 warning(s)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-17 14:38:25 | 显示全部楼层
求助啊 那个地方写错了啊 怎么会报错呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 15:01:28 | 显示全部楼层
char str[N],ss[5]="AEIOU";
char str[N],ss[]="AEIOU";
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 15:36:16 | 显示全部楼层
ss[5]="AEIOU"
五个元素,要用6个位置存放,因为字符串结尾有一个 '\0'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-23 15:28:21 | 显示全部楼层    本楼为最佳答案   
ss[5]是字符串,中间的个数至少要比你输入的字符数多一个,因为字符串会自动在结尾处添加‘\0’,下面是对你的程序稍微修改了一下,并附上了运行结果
#include<stdio.h>
#include<conio.h>
#define N 100
void fun( char *str, int bb[])
{
        char *p = str;
        int i = 0;
        for( i = 0;i < 6; i++ )
                bb[i] = 0;
        while( *p )
        {
                switch( *p )
                {
                case 'A':
                case 'a':bb[0]++;break;
                case 'E':
                case 'e':bb[1]++;break;
                case 'I':
                case 'i':bb[2]++;break;
                case 'O':
                case 'o':bb[3]++;break;
                case 'U':
                case 'u':bb[4]++;break;
                default:bb[5]++;
                }
                p++;
        }
}
int main(void)
{
        char str[N],ss[6]="AEIOU";
        int i;
        int bb[6];
        printf("Input a string :\n");
        gets(str);
        printf("the string is :\n");
        puts(str);
        fun( str, bb );
        for(i = 0; i < 5; i++ )
                printf("\n%c:%d", ss[i], bb[i] );
        printf("\n other:%d \n",bb[i]);
                return 0;
}

/************************************

-------------------------------------
运行结果:
Input a string :
aeiouAEIOU123456789
the string is :
aeiouAEIOU123456789

A:2
E:2
I:2
O:2
U:2
other:9
请按任意键继续. . .


--------------------------------------
**************************************/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 23:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表