C语言(简单的)编写程序输入任意一串字符统计其中大写字母,小写字母。数字及其他字符的个数 C语言编程 从键盘输入一个字符串,分别统计其中大写字母、小写...
c\u8bed\u8a00\u4ece\u952e\u76d8\u4efb\u610f\u8f93\u5165\u4e00\u4e32\u5b57\u7b26\uff0c\u5206\u522b\u7edf\u8ba1\u5176\u4e2d\u5927\u5199\u5b57\u6bcd\uff0c\u5c0f\u5199\u5b57\u6bcd\u4e2a\u6570\u5177\u4f53\u6211\u5c31\u6ca1\u6cd5\u5199\uff0c\u5fd8\u8bb0\u5dee\u4e0d\u591a\u4e86\uff0c\u4f60\u628a\u5b57\u7b26\u4e32\uff0c\u5f04\u5230\u6570\u7ec4\u91cc\uff0c\u7136\u540e\u6bd4\u8f83\u4e0b\uff0c\u5927\u5c0f\u5199\u6709\u4e00\u4e2a\u6bd4\u8f83\u7684\uff0c\u8fd9\u6837\u7edf\u8ba1\u4e0b\u5c31\u53ef\u4ee5\u4e86
#include
void main()
{
int countd=0,countx=0,countk=0,counts=0,countq=0;//\u5206\u522b\u7528\u6765\u5bf9\u5927\u5199\u5b57\u6bcd\u3001\u5c0f\u5199\u5b57\u6bcd\u3001\u7a7a\u683c\u3001\u6570\u5b57\u3001\u5176\u4ed6\u5b57\u7b26\u505a\u8ba1\u6570
char s[100],*p;
printf("\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a");
int i=0;
while((s[i]=getchar())!='\n')i++;
p=&s[0];
while(*p!='\n')
{
if((*p>='A')&&(*p<='Z'))countd++;
else
if((*p>='a')&&(*p<='z'))countx++;
else
if(*p==' ')countk++;// \u2018\u2019\u4e2d\u662f\u4e00\u4e2a\u7a7a\u683c
else
if((*p>='0')&&(*p<='9'))counts++;
else
countq++;
p++;
}
printf("\u5927\u5199\u5b57\u6bcd\u7684\u4e2a\u6570\u4e3a\uff1a%d \u5c0f\u5199\u5b57\u6bcd\u7684\u4e2a\u6570\u4e3a\uff1a%d \u7a7a\u683c\u4e2a\u6570\u4e3a\uff1a%d \n",countd,countx,countk);
printf("\u6570\u5b57\u4e2a\u6570\u4e3a\uff1a%d \u5176\u4ed6\u5b57\u7b26\u4e2a\u6570\u4e3a%d\n",counts,countq);
}
程序可按照以下流程执行:
1、输入字符串
2、对于字符串中的每一个字符,判断其为何种类型,并将相应的累加计数器加1。对于大小写字母和数字,可通过一个范围(大于等于某值和小于等于某值)来判断
代码如下:
#include <string.h>
#include <stdio.h>
int main()
{
char str[1000];
int lowAlpha, upAlpha, num, other;
int i;
lowAlpha = upAlpha = num = other = 0;
for (i = 0; i < strlen(str); i++)
if (str[i] >= 'a' && str[i] <= 'z')
lowAlpha++;
else if (str[i] >= 'A' && str[i] <= 'Z')
upAlpha++;
else if (str[i] >= '0' && str[i] <= '9')
num++;
else
other++;
printf("Upper:%d, Lower:%d, Number:%d, Other:%d
", upAlpha, lowAlpha, num, other);
return 0;
}
#include<stdio.h>
#include<string.h>
#define n 20 //你可以改变n的值,但要保证n至少大于你想输入的字符数目一位
void main()
{
int i,BC,lc,m; //BC为大写的字母,lc为小写的字母
char a[n];
i=BC=lc=m=0;
printf("输入字符串,以#号结束:\t");
gets(a);
while(a[i]!='\0')
{
if(a[i]>='A'&&a[i]<='Z') BC++;
else
if(a[i]>='a'&&a[i]<='z') lc++;
else
m++;
i++;
}
printf("大写字母个数为:%d\n",BC);
printf("小写字母个数为:%d\n",lc);
printf("其他字符个数为:%d\n",m);
}
我调试通过了的
#include <stdio.h>
void count(char*);
int main()
{
char ch[100]={0};
scanf("%s", ch);
count(ch);
return 0;
}
void count(char* ch)
{
//分别记录大写,小写,数字的个数。
int big=0, small=0, character=0,qita = 0;
while (*ch)
{
if ((*ch>='A')&&(*ch<='Z'))
{
++big;
}
else if ((*ch>='a')&&(*ch<='z'))
{
++small;
}
else if ((*ch>='0')&&(*ch<='9'))
{
++character;
}
else
{
++qita;
}
++ch;
}
printf("大写字母的个数是:%d\n", big);
printf("小写字母的个数是:%d\n", small);
printf("数字的个数是:%d\n", character);
printf("其他字符的个数是:%d\n", qita);
}
绛旓細include<stdio.h>int main(){ int a,b; scanf("%d %d",&a,&b); printf("%d*%d+10=%d",a,b,a*b+10);}
绛旓細鍙互鍙傝冧笅闈㈢殑浠g爜锛歩nclude<stdio.h> int main(){int a,b,c,d;int max(int x,int y);printf("璇杈撳叆涓変釜浠绘剰鏁存暟锛歕n");while(scanf("%d %d %d",&a,&b,&c)!=-1){d=max(a,max(b,c))printf("max=%d\n",d);} return 0;} int max(int x,int y){ return x>y?x:y...
绛旓細..1銆佽杈撳叆绗7涓鐢熺殑鎴愮哗锛76 璇疯緭鍏ョ8涓鐢熺殑鎴愮哗锛84 璇疯緭鍏ョ9涓鐢熺殑鎴愮哗锛87 璇疯緭鍏ョ10涓鐢熺殑鎴愮哗锛79 10鍚嶅鐢熺殑骞冲潎鎴愮哗涓猴細830 ```甯屾湜杩欎釜浠g爜鍙互甯姪浣犲疄鐜颁綘鎵闇鐨勫姛鑳姐2銆C璇█锛缂栧啓绋嬪簭锛岃緭鍏10涓鐢熺殑鎴愮哗鏁版嵁锛岃绠楀苟杈撳嚭骞冲潎鍒嗗強浣庝簬骞冲潎鍒嗙殑瀛︾敓鐨勪汉鏁般
绛旓細include<stdio.h> int main() { double x,s=0;char c;int n=0;while(1){ scanf("%lf",&x); s=s+x; n++;if ( (c=getchar())=='\n') break;} printf("%.8f\n",s/n);return 0;} 杈撳叆鏁版嵁鐢ㄧ┖鏍煎垎闅旓紝鏈鍚庝竴涓暟鍚庨潰鏄崲琛岀'\n'锛堝鏋滀綘鐢 鍥炶溅绗 '\r' , 鍒欐敼...
绛旓細浣犲ソ锛佷互涓嬫槸涓涓C璇█绋嬪簭锛屼娇鐢╠o-while寰幆璇彞浠庨敭鐩樹换鎰杈撳叆n涓疄鏁帮紝骞惰绠楀嚭鍏朵腑鐨勬渶澶у煎拰鏈灏忓硷細include <stdio.h> int main() { int n, i = 0;float num, max, min;printf("璇疯緭鍏ヨ杈撳叆鐨勫疄鏁颁釜鏁皀: ");scanf("%d", &n);do { printf("璇疯緭鍏ュ疄鏁: ");scanf("%f", ...
绛旓細鍋囧畾5闄╀竴閲戝浐瀹氭槸1000鍏冿紝璧峰緛鐐规槸3500锛绋嬪簭濡備笅锛歩nclude <stdio.h> int main(int argc, char *argv[]){ float money,temp,total,t;printf("璇杈撳叆宸ヨ祫鏀跺叆锛");scanf("%f",&money);t=money-3500-1000; // 鍏ㄦ湀鏀跺叆鎬婚-1000(浜旈櫓涓閲)-3500(鐜伴樁娈典釜绋庤捣寰佺偣)if (t<1500)temp...
绛旓細棣栧厛鏄庣‘棰樼洰瑕佹眰锛氶掑綊鍑芥暟锛屾眰n!閫掑綊鍑芥暟鐨勫惈涔夛細缂栫▼璇█涓紝鍑芥暟Func(Type a,鈥︹)鐩存帴鎴栭棿鎺ヨ皟鐢ㄥ嚱鏁版湰韬紝鍒欒鍑芥暟绉颁负閫掑綊鍑芥暟銆俷!琛ㄧず闃朵箻鍑芥暟锛屽嵆1*2*3*鈥︹*n 涓嬮潰缁欏嚭浠g爜锛氾紙C璇█瀹炵幇 锛夋瘮杈绠鍗曠殑灏鹃掑綊瀹炵幇锛歩nclude<stdio.h>long digui(int n); //閫掑綊鍑芥暟澹版槑int main(){...
绛旓細include鈥渟tdio.h"void main(){ int a,b,c,sum;float ave;scanf("%d %d %d",&a,&b,&c);sum=a+b+c;ave=(a+b+c)/3.0;printf("涓変釜鏁扮殑鍜屼负锛 %d\n",sum);printf("涓変釜鏁扮殑骞冲潎鍊间负锛 %f\n",ave);}
绛旓細include"stdio.h"//浠庨敭鐩杈撳叆10涓暟瀛樺叆涓缁存暟缁勶紝姹傝繖10涓暟涓殑鏈澶у煎拰鏈灏忓煎苟杈撳嚭 intmain(){ inti;floatmax,min,num[10];printf("璇疯緭鍏10涓暟锛屾瘡杈撳叆涓涓暟鎸夊洖杞﹂敭缁撴潫:\n");for(i=0;i<10;i++){ scanf("%f",&num[i]);} max=min=num[0];for(i=1;i<10;i++){ ...
绛旓細include <stdio.h> int main(void){ char name[20];int age;printf("璇杈撳叆浣犵殑濮撳悕锛");scanf("%s",name);printf("璇疯緭鍏ヤ綘鐨勫勾榫勶細");scanf("%d",&age);printf("浣犵殑濮撳悕鏄細%s\n",name);printf("浣犵殑骞撮緞鏄細%d\n",age);return 0;} ...