C语言:下面图片上的题目咋写?

2025-01-25 12:11:34
推荐回答(2个)
回答1:

代码文本:

#include "stdio.h"

int main(int argc,char *argv[]){

char ch;

int k=0,z=0;

printf("Please enter a string(^z end)...\n");

while((ch=getchar())!=EOF){

if(ch!='\n'){

k+=ch==' ';

z+=ch>='a' && ch<='z' || ch>='A' && ch<='Z';

}

else{

printf("Letter:\t%d\nSpace:\t%d\n\n",z,k);

k=z=0;

printf("Please enter a string(^z end)...\n");

}

}

return 0;

}

回答2:

#include
#include
void input(){
char str[1000];
int i=0,space=0,letter=0;
printf("【样例输入】");
gets(str);
for(i=0;i if(((int)(str[i])>=65&&(int)(str[i])<=90)||((int)(str[i])>=97&&(int)(str[i])<=122)){
letter++;
}else if((int)(str[i])==32){
space++;
}
}
printf("\n【样例输出】");
printf("%d %d\n",letter,space);
}

void main(){
int line=0,i=0;
printf("请输入测试样例的数量:");
scanf("%d",&line);
while(line<=0||line>=1000){
printf("输入的样例数量非法!\n\n");
printf("请输入测试样例的数量:");
scanf("%d",&line);
}
for(i=0;i<=line;i++){
input();
}
}