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

c語言如何統計英文數字個數

發布時間: 2022-04-17 23:44:24

⑴ 救命啊:用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語言問題: 輸入一行字元,分別統計英文字元、數字字元、空格和其它字元的個數

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main() {

char s[100];

int countLetter = 0;

int countNum = 0;

int countSpace = 0;

int countOther = 0;

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

scanf("%[^ ]",s);

for(int i = 0;i< strlen(s);i++){

if((*(s+i)>='A'&&*(s+i)<='Z')||(*(s+i)>='a'&&*(s+i)<='z')) {

countLetter++;

continue;

}

if(*(s+i)>='0'&&*(s+i)<='9') {

countNum++;

continue;

}

if(*(s+i)==' ') {

countSpace++;

continue;

}

else

countOther++;

}

printf("字母有:%d個 數字有:%d個 空格有:%d個 其他字元有:%d個",

countLetter,countNum,countSpace,countOther);

return 0;

}

⑶ c語言統計大小寫字母 數字個數

#include <stdio.h>

#include <stdlib.h>

#define N 100

void func3()

{

char str[N];

int i,lower=0,upper=0,digit=0,space=0;

long others=0;

printf("Input a string:");

gets(str);

for(i=0;str[i]!='';i++)

{

if(str[i]>='a' && str[i]<='z')

lower++; /*統計小寫英文字母*/

else if(str[i]>='A' && str[i]<='Z')

upper++; /*統計大寫英文字母*/

else if(str[i]>='0' && str[i]<='9')

digit++; /*統計字元串*/

else if(str[i]==' ')

space++;

else

others++; /*統計其他字母*/

}

printf("lower English character:%d ",lower);

printf("upper English character:%d ",upper);

printf("digit character:%ld ",digit);

printf("space:%d ",space);

printf("other character: %ld ",others);

return 0;

}

int main()

{

while(1)

{

func3();

printf(" ");

system("pause");

}

return 0;

}

(3)c語言如何統計英文數字個數擴展閱讀:

程序實現思路分析

統計大小寫字母、數字的個數,首先要判斷出字元是屬於哪一種,然後增加計數。

1、判斷

小寫字母的范圍為:'a'~'z'

大寫字母的范圍為:'A'~'Z'

數字的范圍為:'0'~'9'

2、聲明三個int變數並賦值初值為0

lower——統計小寫英文字母

upper——統計大寫英文字母

digit——統計數字

⑷ C語言統計英語單詞的個數

C語言:統計輸入的一行英文句子中的字母及單詞個數,帶註解!

//通過鍵盤輸入一行英文句子,統計其中的英文字母和單詞的數量,單詞之間用空格分開(標點符號不算單詞);
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char string[100];//根據擬從鍵盤輸入的字串的長度需要適當調整,要避免輸入的長度超出設定的范圍。
char c;
int i, num=0,sum=0,word=0; //定義 word 用來指示一個單詞是不是結束或新單詞是否開始;
printf("請從鍵盤輸入一行需要查詢的英文句子,進行單詞數量統計: ");
gets(string); //從鍵盤獲得輸入的字元串;
//以下統計句子中的英文字元個數;
for(i=0;(c=string[i])!='';i++) //for循環語句,遍歷句子中的每個字元;初始化i=0;若字元c!='',即未到達結束符''的話,執行i++;
{
if(('A'<=string[i]&&string[i]<='Z')||('a'<=string[i]&&string[i]<='z'))
sum++; //以上為條件句,如果字元在A~Z,a~z 范圍之內的話,則執行sum++,累加英文字母個數;
}
//以下統計句子中的英文單詞個數;
for(i=0;(c=string[i])!='';i++) //for循環語句,遍歷句子中的每個字元;初始化i=0;若字元c!='',即未到達結束符''的話,執行i++;
{ //''用作字元串的結束符。它的ASCII數值是0。
if(c<'A'||c>'Z'&&c<'a'||c>'z') //設定條件:如果字元 c 遇到A~Z和a~z范圍之外其它符號字元的話,包括遇到空格' ';
word=0; //上面條件為真時,執行這里,置word=0,表示未遇到單詞,或,一個單詞已結束,同時也意味著要開始遇到下一個新單詞;
else if(word==0) //當條件(word==0)為真,執行下面花括弧裡面的語句;當word==0時,表示未遇到字母,即未遇到單詞,或上一個單詞已結束;
{
word=1; //那麼置word=1,即,表示下一個新單詞開始,
num++; //執行num++,累加英文單詞的個數;
}
}
printf(" ");
printf("您輸入的這句英文句子中共包含%d個英文字元,%d個英文單詞。 ",sum,num);

}

示例:輸入語句: Hello ! My friend , how are you ? (注意單詞的前後有空格)

⑸ 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]);

}

(5)c語言如何統計英文數字個數擴展閱讀:

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

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

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

⑹ C語言:輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數

#include<stdio.h>
voidmain()
{
intletter,space,digit,other;
charch;
letter=space=digit=other=0;
while((ch=getchar())!=' ')
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
letter++;
elseif(ch>='0'&&ch<='9')
digit++;
elseif(ch=='')
space++;
else
other++;
}
printf("字母:%d ",letter);
printf("空格:%d ",space);
printf("數字:%d ",digit);
printf("其它字元:%d ",other);
}

⑺ 如何用c語言編寫一段可統計出所輸入的一段字元中的英文字母、空格、數字和其他字元的個數

編程為:

#include<stdio.h>

intmain(){charc[50];inti,el=0,sp=0,nu=0,other=0;gets(c);//輸入字元串for(i=0;i<strlen(c);i++)//strlen返回字元串長度{if((c[i]>='A'&&c[i]<='Z')||(c[i]>='a'&&c[i]<='z'))el++;elseif(c[i]>='0'&&c[i]<='9')nu++;elseif(c[i]=='')sp++;elseother++;}printf("英文字母個數=%d 數字個數=%d 空格字數=%d 其他字元個數=%d ",el,nu,sp,other);return0;}