當前位置:首頁 » 編程語言 » c語言用程序統計英文字母數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言用程序統計英文字母數

發布時間: 2022-06-14 03:51:10

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!='';p++)

{

if(('a'<=*p && *p<='z') || ('A'<=*p && *p<='Z'))

{

n++;

cout<<*p<<" ";

}

else

break;

}

cout<<"字元串的數量為:"<<n<<endl;

return 0;

}

(3)c語言用程序統計英文字母數擴展閱讀:

printf用法:

printf()函數的調用格式為:printf("&lt;格式化字元串&gt;",&lt;參量表&gt;)。

其中格式化字元串包括兩部分內容:一部分是正常字元,這些字元將按原樣輸出;另一部分是格式化規定字元,以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。

參量表是需要輸出的一系列參數,其個數必須與格式化字元串所說明的輸出參數個數一樣多,各參數之間用","分開,且順序一一對應,否則將會出現意想不到的錯誤。

比如:

inta=1234;

printf("a=%d\n",a);

輸出結果為a=1234。

D. 任意輸入一行英文字元,統計出其中英文字母的個數。 跪求用C語言編程!

#include
int
main(){
char
c[50];
int
i,el=0,sp=0,nu=0,other=0;
gets(c);//輸入字元串
for(i=0;
i
='a'
&&
c[i]<='z')||(c[i]>='a'
&&
c[i]<='z'))
el++;
else
if(c[i]>='0'&&c[i]<='9')
nu++;
else
if(c[i]=='
')
sp++;
else
other++;
}
printf("英文字母個數=%d\n數



=%d\n空



=%d\n其他字元個數=%d\n",el,nu,sp,other);
return
0;}已經測試過了,測試結果如下,有問題可以繼續追問。

E. C語言簡單編程(字母數統計)

#include<stdio.h>
int counte;
int counts;
int countn;
int counto;
void count(char a[])
{
int i;
counte=0;
counts=0;
countn=0;
counto=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
countn++;
else if(a[i]==' ')
counts++;
else if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
counte++;
else
counto++;
}

}
main()
{
char str[100];
gets(str);
count(str);
printf("e=%d,s=%d,n=%d,o=%d",counte,counts,countn,counto);
}

其中 count函數的 else if(a[i]==' ')寫錯了,這個才是,『=』是賦值,==才是比較。

F. c語言中怎樣統計字元串中包含英文字母的個數

c語言中要統計字元串中包含英文字母的個數可以參考以下內容:

main()

{

char str[100],*p;

int num[4],i;

p=str;

gets(str);

for(i=0;i<4;i++)

num[i]=0;

for(;*p!='';p++)

{

if((*p<='z'&&*p>='a')||(*p<='Z'&&*p>='A')) num[0]++;

else if(*p==' ') num[1]++;

else if((*p<='9'&&*p>='0')) num[2]++;

else num[3]++;

}

printf("%d %d %d %d ",num[0],num[1],num[2],num[3]);

}

(6)c語言用程序統計英文字母數擴展閱讀:

在寫代碼的過程中需要注意:

void main()的用法並不是任何標准制定的。 C語言標准語法是int main,任何實現都必須支持int main(void) { /* ... */ }和int main(int argc, char* argv[]) { /* ... */ }。

類似於a+=a++;或者(i++)+(i++)+(i++)屬於未定義行為,並不是說c語言中還未定義這種行為,它早有定論,它的結果取決於編譯器實現,不要寫這樣的代碼。

G. C語言編程:輸入一行字元,統計其中英文字母的個數

#include<stdio.h>

int main()

{char s[200];

int i,n=0;

gets(s);

for(i=0;s[i];i++)

if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')n++;

printf("%d ",n);

getch();

return 0;

}

H. C語言編程,用while語句,輸入一行字元統計字母的個數

代碼如下:

#include <stdio.h>

int main()

{

char c;

int letters=0,space=0,digit=0,other=0;

printf("請輸入一行字元:");

while ((c=getchar())!=' ')

{

if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')

{

letters++;

}

else if (c == ' ')

{

space++;

}

else if (c >= '0'&&c <= '9')

{

digit++;

}

else

{

other++;

}

}

printf("字母數:%d 空格數:%d 數字數:%d 其他字元:%d ",letters,space,digit,other);

return 0;

}

(8)c語言用程序統計英文字母數擴展閱讀

while的執行順序

while 循環的執行順序非常簡單,它的格式是:

while (表達式)
{
語句;
}

當表達式為真,則執行下面的語句;語句執行完之後再判斷表達式是否為真,如果為真,再次執行下面的語句;然後再判斷表達式是否為真……就這樣一直循環下去,直到表達式為假,跳出循環。這個就是 while 的執行順序。

注意,初學者編程時,if、else、for、while、do 後面的執行語句不論有多少行,就算只有一行也要加「{}」,養成良好的編程習慣尤為重要。

再來看一下 for 循環的格式:

for (表達式1;表達式2;表達式3)

在 for 循環的格式中,表達式 1、表達式 2 和表達式 3 在 while 循環中一個也不少,只不過不像 for 循環那樣寫在一起,而是分開寫。在 while 循環中,循環變數 i 在定義的時候就給它賦初值,++i 則是寫在 while 的循環體內。只有循環判斷表達式與 for 一樣,都是寫在其後的括弧中。

並且所有的 for 循環都可以轉化成 while 循環,不僅如此,所有的 while 循環也都可以轉化成 for 循環,for 循環和 while 循環可以相互轉換。

I. C語言編程:輸入一串字母,統計每個字母出現的次數

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;

}

(9)c語言用程序統計英文字母數擴展閱讀:

程序思路:
分為三部分 首先輸入字元串 ,其次設定一個字元數組英文小寫字母24, 同時設一個int數組 記錄個數, 以及一個int c 為了給int數組賦值。最後在輸入的時候進行判斷,如果字母的值 大於等於1才輸出。

J. 用c語言編程,字元統計:輸入一個文本文件,分別統計出其中英文字母、空格、數字和其它字元的個數

#include <stdio.h>

int main()

{

char c;

int letters=0,space=0,digit=0,other=0;

printf("請輸入一行字元:");

while ((c=getchar())!=' ')

{

if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')

{

letters++;

}

else if (c == ' ')

{

space++;

}

else if (c >= '0'&&c <= '9')

{

digit++;

}

else

{

other++;

}

}

printf("字母數:%d 空格數:%d 數字數:%d 其他字元:%d ",letters,space,digit,other);

return 0;

}

運行效果:

(10)c語言用程序統計英文字母數擴展閱讀

printf函數使用注意事項

1、域寬

%d:按整型數據的實際長度輸出。

如果想輸出指定寬度可以指定域寬,%md--&gt;m域寬,列印出來以後,在控制台上,顯示m位;

如果我們要列印的數的位數如果超過我們設定m則原樣輸出;

如果我們要列印的數的位數如果小於我們設定的位數,則補空白,具體如下:

如果m為正數,則左對齊(左側補空白);

如果m為負數,則右對齊(右側補空白)。

2、轉義字元

如果想輸出字元"%",則應該在「格式控制」字元串中用連續兩個%表示。

如:printf("%f%%",1.0/3);輸出結果:0.333333%。