分类统计字符串中元音字母和其他字符的个数
#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=0;
while(*p)
{
switch(*p)
{
case 'A':
case 'a':bb++;break;
case 'E':
case 'e':bb++;break;
case 'I':
case 'i':bb++;break;
case 'O':
case 'o':bb++;break;
case 'U':
case 'u':bb++;break;
default:bb++;
}
p++;
}
}
main()
{
char str,ss="AEIOU";
int i;
int bb;
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,bb);
printf("\n other:%d",bb);
} --------------------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)
求助啊 那个地方写错了啊 怎么会报错呢 char str,ss="AEIOU";
char str,ss[]="AEIOU";
ss="AEIOU"
五个元素,要用6个位置存放,因为字符串结尾有一个 '\0' ss是字符串,中间的个数至少要比你输入的字符数多一个,因为字符串会自动在结尾处添加‘\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 = 0;
while( *p )
{
switch( *p )
{
case 'A':
case 'a':bb++;break;
case 'E':
case 'e':bb++;break;
case 'I':
case 'i':bb++;break;
case 'O':
case 'o':bb++;break;
case 'U':
case 'u':bb++;break;
default:bb++;
}
p++;
}
}
int main(void)
{
char str,ss="AEIOU";
int i;
int bb;
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, bb );
printf("\n other:%d \n",bb);
return 0;
}
/************************************
-------------------------------------
运行结果:
Input a string :
aeiouAEIOU123456789
the string is :
aeiouAEIOU123456789
A:2
E:2
I:2
O:2
U:2
other:9
请按任意键继续. . .
--------------------------------------
**************************************/
页:
[1]