c++改错2

来源:百度知道 编辑:UC知道 时间:2024/05/26 19:39:22
下列给定程序中,函数fun的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符串。如:”ab12cdef3d45”运算后的结果是”12345”
# include<iostream.h>
void fun(char *s)
{ int i,j;
for(i=0,j=0; s[i]!=’\0’; i++)
if(s[i]>=’0’&& s[i]!<=’9’)
s[j]=s[i];
s[j]=”\0”;
}
void main(){
char as[]=”ab12cdef3d45”;
fun(as);
cout<<”cs=”<<cs<<endl;
}
38下列给定程序中,fun函数的功能是:分别统计字符串中大写字母和小写字母的个数。例如,给字符串s输入:AaaaBBb123CCccccd,则应输出结果:upper=6,lower=8.
# include<iostream.h>
void fun ( char *s,int a,int b)
{
while (*s )
{ if (*s>=’A’&&*s<=’Z’)
a++ ;
if (*s>=’a’&& *s<=’z’)
b++;
s++;
}
}
void main(){
char as[]=” AaaaBBb123CCccccd”;
int a=0,b=0;
fun(as,a,b);
cout<<”upper=”<<a<<” lower= “<<b<<endl;
}
注意:子函数有错,主函数main无错。

第一题:
void fun(char *s)
{
char s1[128];
int i,j;
for(i=0,j=0; s[i] != '\0'; i++)
if(s[i] >= '0'&& s[i] <='9')
{
s1[j] = s[i];
++j;
}
s1[j] = '\0';
}

第二题:
void fun ( char *s,int *a,int *b)
{
while (*s)
{
if (*s>= 'A' && *s <= 'Z')
(*a)++;
if (*s >= 'a'&& *s <= 'z')
(*b)++;
s++;
}
}
void main()
{
char as[] = "AaaaBBb123CCccccd";
int a=0,b=0;
fun(as,&a,&b);
cout<< "upper="<<a<<"lower= "<<b<<endl;
}
这个我说一句,这个题一定要把a,b的地址传进去,和scanf("%d",&a);道理一样。