c++编写一个 函数,统计一条英文句子中字母的个数,在主程序中实现输入、输出。

我编的程序是这样的:
#include<iostream.h>
#include<string.h>
int count(char a[]);
int main()
{
int d;
char c[];
char b;
cin>>b;
strcpy(c,b);
d=count(c);
cout<<d;
return 0;
}
int count(char a[])
{
int b=0,c=0;
do
{
if((a[c]>'a'&&*a[c]<'z')||(a[c]>'A'&&*a[c]<'Z'))
b++;
c++;
}while(a[c]!='\0');
return b;
}
编译时出现了错误:
C:\VC6\MyProjects\shiyan5dian6\进阶3.cpp(7) : error C2133: 'c' : unknown size
C:\VC6\MyProjects\shiyan5dian6\进阶3.cpp(10) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\VC6\MyProjects\shiyan5dian6\进阶3.cpp(20) : error C2100: illegal indirection
C:\VC6\MyProjects\shiyan5dian6\进阶3.cpp(20) : error C2100: illegal indirection
执行 cl.exe 时出错.

shiyan5dian6.exe - 1 error(s), 0 warning(s)
怎么在现有基础上改正?

char c[]实际上等价于char *c,这时候你的c指针没有初始化,有两个方法,一个是
char c[100]这种直接初始化,另一个是char *c; c=new char[100];采用动态分配内存的方法(最后要delete[] c)。
温馨提示:答案为网友推荐,仅供参考
相似回答