A. c語言 編寫程序,從鍵盤輸入若干個英文字母,並統計各字母出現的次數
#include
#include
#define
max
100
int
main()
{
char
str[max];
//
輸入的字元串,最大長度是max-1,因為有一個字元串結束符
int
i
=
0,
count[52]
=
{
0
};
//
count
數組用來存儲各個字母出現的次數
scanf("%s",
str);
while(str[i]
!=
'\0')
{
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
統計小寫字母
count[str[i]
-
97
+
26]++;
}
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
統計大寫字母
count[str[i]
-
65]++;
}
i++;
}
for
(i
=
0;
i
<
26;
i++)
{
//
輸出大寫字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
65,
count[i]);
//
只輸出不為零的數據
}
for
(i
=
26;
i
<
52;
i++)
{
//
輸出小寫字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
97
-
26,
count[i]);
}
return
0;
}
B. 救命啊:用C語言編程「任意輸入一行英文字母,統計出其中英文字母的個數 」怎麼弄
#include <stdio.h>
void main()
{
char c;
int letter=0;
printf("請輸入一行字元:\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
}
printf("英文字母的個數:%d\n",letter);
}
C. C語言編程:輸入一串英文字母,統計每個字母(不區分大小寫)出現的次數
#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!='