C语言编程:输入一串字母,统计每个字母出现的次数 c语言 输入一串字符,以'?'结束,统计各字母出现的次数
C\u8bed\u8a00\u7f16\u7a0b\uff1a\u8f93\u5165\u4e00\u4e32\u82f1\u6587\u5b57\u6bcd,\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u6bcd(\u4e0d\u533a\u5206\u5927\u5c0f\u5199)\u51fa\u73b0\u7684\u6b21\u6570#include
#include
using namespace std;
int main()
{
char str[50];
int n=0;
char *p;
p=str;
cout<<"\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32:"<<endl;
cin>>str;
for(int i=0;*p!='\0';p++)
{
if(('a'<=*p && *p<='z') || ('A'<=*p && *p<='Z'))
{
n++;
cout<<*p<<" ";
}
else
break;
}
cout<<"\u5b57\u7b26\u4e32\u7684\u6570\u91cf\u4e3a:"<<n<<endl;
return 0;
}
\u6269\u5c55\u8d44\u6599\uff1a
printf\u7528\u6cd5\uff1a
printf\uff08\uff09\u51fd\u6570\u7684\u8c03\u7528\u683c\u5f0f\u4e3a\uff1aprintf\uff08\uff02\uff06lt\uff1b\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff06gt\uff1b\uff02\uff0c\uff06lt\uff1b\u53c2\u91cf\u8868\uff06gt\uff1b\uff09\u3002
\u5176\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5305\u62ec\u4e24\u90e8\u5206\u5185\u5bb9\uff1a\u4e00\u90e8\u5206\u662f\u6b63\u5e38\u5b57\u7b26\uff0c\u8fd9\u4e9b\u5b57\u7b26\u5c06\u6309\u539f\u6837\u8f93\u51fa\uff1b\u53e6\u4e00\u90e8\u5206\u662f\u683c\u5f0f\u5316\u89c4\u5b9a\u5b57\u7b26\uff0c\u4ee5\uff02\uff05\uff02\u5f00\u59cb\uff0c\u540e\u8ddf\u4e00\u4e2a\u6216\u51e0\u4e2a\u89c4\u5b9a\u5b57\u7b26\uff0c\u7528\u6765\u786e\u5b9a\u8f93\u51fa\u5185\u5bb9\u683c\u5f0f\u3002
\u53c2\u91cf\u8868\u662f\u9700\u8981\u8f93\u51fa\u7684\u4e00\u7cfb\u5217\u53c2\u6570\uff0c\u5176\u4e2a\u6570\u5fc5\u987b\u4e0e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6240\u8bf4\u660e\u7684\u8f93\u51fa\u53c2\u6570\u4e2a\u6570\u4e00\u6837\u591a\uff0c\u5404\u53c2\u6570\u4e4b\u95f4\u7528\uff02\uff0c\uff02\u5206\u5f00\uff0c\u4e14\u987a\u5e8f\u4e00\u4e00\u5bf9\u5e94\uff0c\u5426\u5219\u5c06\u4f1a\u51fa\u73b0\u610f\u60f3\u4e0d\u5230\u7684\u9519\u8bef\u3002
\u6bd4\u5982\uff1a
inta\uff1d1234\uff1b
printf\uff08\uff02a\uff1d\uff05d\uff3cn\uff02\uff0ca\uff09\uff1b
\u8f93\u51fa\u7ed3\u679c\u4e3aa\uff1d1234\u3002
count[26]={0}\u4ee3\u886826\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\uff0cwhile((c=getchar())!='?')
{
if(c>='a'&&c<='z')
count[c-'a']++;
}
while\u91cc\u9762\u7684\u662f\u5224\u65ad\u5faa\u73af\u662f\u5426\u7ed3\u675f\u7684\u8bed\u53e5 if\u8bed\u53e5\u5219\u5224\u65ad\u8bfb\u5165\u7684c\u662f\u5426\u662f\u5b57\u6bcd\uff0c\u5982\u679c\u662f\uff0c\u5219count[c-'a']++;count\u3010c-'a'\u3011\u5c31\u662f\u8be5\u5b57\u6bcd \u6bd4\u5982\u8bfb\u5165a a-a\u5c31\u662f0 count[0]\u5c31\u4ee3\u8868a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570 \u5176\u4f59\u540c\u7406
C语言程序如下:
#include<stdio.h>
int main()
{
char a[100];
char b[24];
int s[100] = { 0 };//用于存储字符的个数
gets(a);//输入字符
//开始比较
for (int x = 0; x < 24; x++)
{
int c = 0;//记录每个字符个数
b[x] = x + 97;//为了让b[0]是a,b[1]是b依次类推
for (int i = 0; i < 100; i++)
{
if (b[x] == a[i])
{
++c;
s[x] = c;
}
}
if (s[x]>=1)//只输出输入中有的字母 的个数
{
printf("%c %d
", b[x], s[x]);
}
}
getchar();
return 0;
}
扩展资料:
程序思路:
分为三部分 首先输入字符串 ,其次设定一个字符数组英文小写字母24, 同时设一个int数组 记录个数, 以及一个int c 为了给int数组赋值。最后在输入的时候进行判断,如果字母的值 大于等于1才输出。
#include<stdio.h>
void main()
{
char ch; //ch用来每次接收一个字符
int i,s[26]={0}; //数组s[]用来统计每个小写字母的个数
printf("please input a string:\n");
scanf("%c",&ch);
while(ch!='\n') //输入一行字符,以回车键结束
{
if(ch>'A'&&ch<'Z') //遇到大写字母时,转换成小写来处理
ch+=32;
s[ch-'a']++; //0~25对应a~z
scanf("%c",&ch);
}
printf("\n");
for(i=0;i<26;i++)
if(s[i]) //只输出输入过的字母统计
printf("%c:%d\n",(char)(i+'a'),s[i]);
//(char)(i+'a')用强制类型转换,输出对应字母
}
绛旓細棰樼殑绛旀 include <stdio.h> int main(){ char str[121] ;int cnt = 0 ;//鐢ㄤ簬缁熻澶у啓瀛楁瘝涓暟 int i = 0 ;printf("璇杈撳叆涓涓鑻辨枃瀛楁瘝\n") ;gets(str) ;while(str[i]!='\0'){ if(str[i]>='A'&&str[i]<='Z')cnt++ ;i++ ;} printf("澶у啓瀛楁瘝涓鍏辨湁[%d]涓猏n",cnt...
绛旓細strncpy(wac->str1[wac->strC],str+j,i-j);wac->strC++;} printf("total=%d\n",wac->strC);//return(sum);} void main(){ char str[1000];WordAndCnt wac;memset(str,0,1000);printf("杈撳叆涓涓鑻辨枃锛歕n");gets(str);total(str,&wac);printf("\n\n");if(wac.strC>0){ ...
绛旓細2013-03-22 C璇█棰樼洰,浠庨敭鐩樿緭鍏ヤ竴琛屾枃鏈,缁熻鍏朵腑姣忎釜鑻辨枃瀛楁瘝鍑虹幇鐨勯... 43 2012-12-25 姹俢璇█ 鎸囩偣!!!浠庨敭鐩樿緭鍏ヤ竴涓茶嫳鏂瀛楁瘝,缁熻姣忎釜瀛楁瘝鐨勪釜... 15 2007-06-23 鐢C璇█缂栫▼:杈撳叆涓琛瀛楃,鍒嗗埆缁熻鍑哄叾涓嫳鏂囧瓧姣嶃佺┖鏍笺佹暟... 96 2007-04-28 璇曠紪绋:杈撳叆涓琛屽瓧绗,鍒嗗埆缁熻鍑哄叾涓嫳鏂囧瓧姣,...
绛旓細int c = 0;//璁板綍姣忎釜瀛楃涓暟 b[x] = x + 97;//涓轰簡璁゜[0]鏄痑锛宐[1]鏄痓渚濇绫绘帹 for (int i = 0; i < 100; i++){ if (b[x] == a[i]){ ++c;s[x] = c;} } if (s[x]>=1)//鍙緭鍑杈撳叆涓湁鐨瀛楁瘝 鐨勪釜鏁 { printf("%c %d\n", b[x], s[x]);} ...
绛旓細杈撳叆涓琛瀛楃鍒嗗埆缁熻锛鍑哄叾涓嫳鏂囧瓧姣嶇┖鏍兼暟瀛楀拰鍏朵粬瀛楃鐨勪釜鏁扮殑婧愪唬鐮佸涓嬶細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...
绛旓細include<stdio.h> void main(){ int countd=0,countx=0,countk=0,counts=0,countq=0;//鍒嗗埆鐢ㄦ潵瀵瑰ぇ鍐瀛楁瘝銆佸皬鍐欏瓧姣嶃佺┖鏍笺佹暟瀛椼佸叾浠栧瓧绗﹀仛璁℃暟 char s[100],*p;printf("璇杈撳叆涓涓瓧绗︿覆锛");int i=0;while((s[i]=getchar())!='\n')i++;p=&s[0];while(*p!='\n'){ ...
绛旓細婧绋嬪簭浠g爜濡備笅锛歩nclude "pch.h"define _CRT_SECURE_NO_WARNINGS//VS鐜涓嬮渶瑕侊紝VC涓嶉渶瑕 include<stdio.h> int main(){ char c = 0;//瀹氫箟杈撳叆瀛楃鍙橀噺 int num_count = 0;//鏁板瓧涓暟 int bigalp_count = 0;//澶у啓瀛楁瘝涓暟 int littlealp_count = 0;//灏忓啓瀛楁瘝涓暟 int emp_count...
绛旓細else if(a[i]>='a'&&a[i]<='z')濡傛灉绗琲涓瓧绗︾殑ASCII鍊煎湪97鍜122涔嬮棿锛堝皬鍐欏瓧姣峚-z锛夛紝鍒欏垽鏂负瀛楁瘝锛泎++;else q++;濡傛灉閮戒笉绗﹀悎鏉′欢锛屽氨鍒ゅ畾涓哄叾浠栧瓧绗︼紱puts(a);鎶奱鏁扮粍鍒版祦stdout涓 printf("鏁板瓧:%d,绌烘牸:%d,瀛楁瘝:%d,鍏朵粬:%d",s,k,z,q); 浠10杩涘埗鐨勫舰寮忚緭鍑烘墍鏈缁熻涓暟 ...
绛旓細"杈撳叆锛%s\n",str);//杈撳叆鎴戝氨鎳掑緱鎵嬭緭浜嗭紝鐩存帴鍒濆硷紝浣犺鎵嬭緭锛屽姞涓猻canf鍚 for(i=0;i<52;i++) for(j=0;j<strlen(str);j++) { if(cASCII[i][0]==str[j]) cASCII[i][1]++; } for(
绛旓細include<stdio.h> void main(){ char a[100];int sum0=0,suma=0,sumA=0;gets(a);char*p;for(p=a;*p!='\0';p++){ if(*p>='0'&&*p<='9')sum0+=1;else if(*p>='a'&&*p<='z')suma+=1;else if(*p>='A'&&*p<='Z')sumA+=1;} printf("鏁板瓧瀛楃鏁伴噺锛%d\n灏忓啓瀛楁瘝...