C语言编程:输入一串字母,统计每个字母出现的次数
\u7f16\u7a0b\u7279\u60e0\u6765\u88ad,\u4ef7\u4f4e\u8d28\u4f18\u8d2d\u5f97\u5b89,\u670d\u52a1\u5b8c\u5584\u5fc3\u66f4\u751c #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')用强制类型转换,输出对应字母
}
绛旓細鍦C璇█涓紝缂栧啓涓涓▼搴忓彲浠缁熻骞惰緭鍑虹粰瀹氬瓧绗︿覆涓殑澶у啓瀛楁瘝銆佸皬鍐欏瓧姣嶃佹暟瀛楀瓧绗﹀拰鍏朵粬瀛楃鐨勬暟閲忋傜▼搴忎娇鐢ㄦ寚閽堥亶鍘嗗瓧绗︿覆锛岄氳繃鏉′欢鍒ゆ柇鏉ュ尯鍒嗗悇绫诲瓧绗︺備互涓嬫槸璇ョ▼搴忕殑绀轰緥浠g爜锛歩nclude<stdio.h>voidmain(){chara[100];intsum0=0,suma=0,sumA=0;gets(a);char*p;for(p=a;*p!='\0';p++)...
绛旓細1銆佽鍏ュ瓧绗︼紝鐩村埌閬囧埌鎹㈣缁撴潫銆2銆佸浜庢瘡涓瓧绗︼紝鍒ゆ柇鏄瀛楁瘝杩樻槸鏁板瓧锛屾垨鑰呮槸鍏跺畠瀛楃銆3銆佸浜庢瘡涓瓧绗﹀垽鏂悗锛屽搴旂被鍒鏁板櫒鑷姞銆4銆佹渶缁堣緭鍑虹粨鏋溿備笁銆佸弬鑰冧唬鐮侊細include <stdio.h>int main(){ int a,b,c,ch; a=b=c=0;//璁℃暟鍣ㄥ垵濮嬪寲涓0. while((ch=getchar())!='\n')...
绛旓細include <stdio.h>int main(){int a[128]={0}; char ch; do {scanf("%c",&ch); a[ch]++; }while(ch!='\n'); for(ch='A';ch<='Z';ch++) if(a[ch])cout<<ch<<":"<<a[ch]<<endl; for(ch='a';ch<='z';ch++) if(a[ch])cout<<ch<<":"<<a[ch]<...
绛旓細void frequency( string& s, char& A[ ], int& C[ ], int &k ) { // s鏄杈撳叆瀛楃涓,鏁扮粍A[ ]涓褰曞瓧绗︿覆涓湁澶氬皯绉嶄笉鍚岀殑瀛楃,C[ ]涓褰曟瘡 //涓绉嶅瓧绗︾殑鍑虹幇娆℃暟銆傝繖涓や釜鏁扮粍閮藉簲鍦ㄨ皟鐢绋嬪簭涓畾涔夈俴杩斿洖涓嶅悓瀛楃鏁般 int i, j, len = s.length( ); if ( !len ) { ptintf("...
绛旓細int main(){char a[100],*p;void can(char *p);printf("杈撳叆涓涓瓧绗︿覆");p=a;gets(a);can(p);printf("澶у啓瀛楁瘝%d涓猏n",j);printf("灏忓啓瀛楁瘝%d涓猏n",k);printf("鏁板瓧%d涓猏n",l);return 0;} void can(char *p){for(;*p;p++)if((*p>='a')&&(*p<='z'))++k;else if...
绛旓細浠g爜濡備笅锛/*缂栫▼浠庨敭鐩杈撳叆涓涓瓧绗︿覆锛岀粺璁璇ュ瓧绗︿覆涓粠'a'鍒'z'鍏26涓皬鍐瀛楁瘝鍚勮嚜鍑虹幇鐨勬鏁帮紝灏嗙粨鏋滃瓨鍏ユ暟缁勪腑锛屽苟杈撳嚭*/ include<stdio.h> include<string.h> define N 100 main(){int i,j=0,c[26]={0};char str[N];printf("璇疯緭鍏ヤ竴涓瓧绗︿覆锛");gets(str);for(i=0;i<...
绛旓細C璇█绋嬪簭濡備笅锛歩nclude<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;//涓轰簡璁゜[0]鏄痑锛宐[1]鏄痓渚濇...
绛旓細鍙互浣跨敤浠ヤ笅浠g爜杩涜缁熻锛include main(){undefined int acount=0,bcount=0,ccount=0,dcount=0;char a;printf("璇杈撳叆涓琛瀛楃:\n");a = getchar();while (a !='\n'){undefined switch (a){undefined case'q':case'w':case'e':case'r':case't':case'y':case'u':case'i':cas...
绛旓細othCnt; /*鍒嗙被璁℃暟*/capCnt = lowCnt = numCnt = spaceCnt = othCnt = 0;puts ("杈撳叆瀛楃涓诧細");gets (str);while (*p) {if (*p>='A'&&*p<='Z') {cap[capCnt] = *p;capCnt++;}else if (*p>='a'&&*p<='z') {low[lowCnt] = *p;lowCnt++;}else if (*p>='0'...
绛旓細include<iostream> include<string> using namespace std;int main(){ char str[50];int n=0;char *p;p=str;cout<<"璇杈撳叆瀛楃涓:"<<endl;cin>>str;for(int i=0;*p!='\0';p++){ if(('a'<=*p && *p<='z') || ('A'<=*p && *p<='Z')){ n++;cout<<*p<<" ";} el...