#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;
}