谭浩强C语言第五版的一些课后习题

第七章 第九题

统计字符串中的字母,数字,空格和其他(应用gets而不是scanf)

int zm=0,num=0,space=0,other=0;
int main(){
	void count(char s[]);
	char str[40];
	printf("输入字符串:\n");
	gets(str);
	count(str);
	printf("字母个数为:%d 数字个数为:%d 空格个数为:%d 其他个数为:%d\n",zm,num,space,other);
}
void count(char s[]){
	int i=0;
	while(s[i]!='\0'){
		if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
			zm++;
		}
		else if(s[i]>='0'&&s[i]<='9'){
			num++;
		}
		else if(s[i]==' '){
			space++;
		}else{
			other++;
		}
		i++;
	}
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐