统计大写、小写、空格、数字、其他字符

#include<stdio.h>
int len(char *s){ 
	int i=0;
	while (*s++) 
	i++;
	return(i);
}	
int main(){ 
	char *p,str[100];
	int l,i,a,b,c,d,e;
	for (i=0;;i++){ 
		scanf("%c",&str[i]);
		if (str[i]=='\n') break;
	}
	str[i]='\0';
	p=str;
	l=len(p);//strlen()
	for (i=0;i<l;i++)
		printf("%c",*(p+i));//str[i]=*(p+i)
	for (i=a=b=c=d=e=0;i<l;i++){ 
	if (*(p+i)==' ') 
		c++;
	else if (*(p+i)>='A'&&*(p+i)<=90) a++;
	else if (*(p+i)>='a'&&*(p+i)<='z') b++;
	else if (*(p+i)>='0'&&*(p+i)<='9') d++;
	else e++;
	}
	printf(" 大写字母 %d 个,小写字母 %d 个,空格 %d个,数字 %d 个,其他字符 %d 个\n",a,b,c,d,e);
}

解法2

#include<stdio.h>
int main() {
    char c;
    int letters=0,spaces=0,digits=0,others=0;
    printf("请输入一些字母:\n");
    while((c=getchar())!='\n') {
        if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
            letters++;
        else if(c>='0'&&c<='9')
            digits++;
        else if(c==' ')
            spaces++;
        else
            others++;
    }
    printf("字母=%d,数字=%d,空格=%d,其他=%d\n",letters,digits,spaces,others);
    return 0;
}

输入一行字符,统计其中有多少单词,单词之间用空格分隔开

#include <stdio.h>
int main(){
	int i,num=0,word=0;
	char string[80];
	char c;
		gets(string);
	for(i=0;(c=string[i])!='\0';i++)
		if(c==' ')word=0;
		else if(word==0)
		{word=1;
	     num++;
	    }
	printf("there is %d words in this line;\n",num);
	return 0;
}

一维数组查找最大最小数

#include<stdio.h>
float max (float *a){
	int i,j,t;
	for(i=0;i<10-1;i++)
		for (j=0;j<10-i-1;j++)
			if(a[i]>a[i+1]) {
				t=a[i];a[i]=a[i+1];a[i+1]=t;
			}
	return a[9];//a[9]最大a[0]最小
}
float min(float *a){
	float t;
	int i,j;
	for(j=0;j<9;j++)
		for(i=0;i<9-j;i++){
			if(a[i]>a[i+1]){
				t=a[i];a[i]=a[i+1];a[i+1]=t;
			}
		}
	return a[0];
}
	int main(){
	float a[10],x,y;int i;
	for (i=0;i<10;i++) 
		scanf("%f",&a[i]);
	x=max(a);
	y=min(a);
	printf("%f\n",x);
	printf("%f\n",y);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值