當前位置:首頁 » 編程語言 » c語言統計字母個數

c語言統計字母個數

發布時間: 2023-05-09 22:05:34

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

}

(1)c語言統計字母個數擴展閱讀:

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

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

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

② C語言 數組 統計英文字母個數

你好像沒有對字母排序。試試這個,比你的簡單,設一個26位的數組,掃描一遍字困念符串,是第幾位英文字母,就在數組的鏈段第幾號汪喚困元素加1。最後輸出數組非0元素就行了。
#include <stdio.h>
void main()
{
int a[26]={0},i;
char x[50],*p=x;
bool flag=true;
gets(x);
while(*p)

{
if(*p>='a' && *p<='z')
a[*p-'a']++;
if(*p>='A' && *p<='Z')
a[*p-'A']++;
p++;
}
for(i=0;i<26;i++)
{
if(a[i])
{
if(flag)
{
printf("%d",a[i]);
flag=false;
}
else
printf(" %d",a[i]);
}
}
printf("\n");
}

③ c語言統計字元串中字母個數是多少

可以使用以下代碼進行統計:

#include

main()

{undefined

intacount=0,bcount=0,ccount=0,dcount=0;

chara;

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

a=getchar();

while(a!=' ')

{undefined

switch(a)

{undefined

case'q':

case'w':

case'e':

case'r':

case't':

case'y':

case'u':

case'i':

case'o':

case'p':

case'a':

case's':

case'd':

case'f':

case'g':

case'h':

case'j':

case'k':

case'l':

case'z':

case'x':

case'c':

case'v':

case'b':

case'n':

case'm':

case'Q':

case'W':

case'E':

case'R':

case'T':

case'Y':

case'U':

case'I':

case'O':

case'P':

case'A':

case'S':

case'D':

case'F':

case'G':

case'H':

case'J':

case'K':

case'L':

case'Z':

case'X':

case'C':

case'V':

case'B':

case'N':

case'M':

acount++;break;

case'1':

case'2':

case'3':

case'4':

case'5':

case'6':

case'7':

case'8':

case'9':

case'0':

bcount++;break;

case'':

ccount++;break;

default:

dcount++;break;

}

a=getchar();

}

printf("字母數:%d 空格數:%d 數字數:%d 其他字元:%d ",acount,ccount,bcount,dcount);

}

分組編寫函數:

編寫一個函數void fun(char *tt,int pp[]),統計在tt字元中"a"到"z"26各字母各自出現的次數,並依次放在pp所指的數組中。

#include<stdio.h>

#include<string.h>

void fun(char *tt,int pp[]);

void main()

{

char tt[50];

int pp[26]={0};

printf("Input a string: ");

gets(tt);

fun(tt,pp);

}

void fun(char *tt,int pp[])

{

int i;

for(i=0;i<strlen(tt);i++)

{

if(*(tt+i) >= 'a' && *(tt+i) <= 'z')

pp[*(tt+i)-'a']++;

}

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

printf("%c appeared %d times ",'a'+i,pp[i]);

}

④ C語言中求輸入若干字元統計其中字母及數字字元的個數

C語言中求輸入若干字元統計其中字母及數字字元的個數 用下面的迴圈加判斷就可以統計其中的字母和數字的個數:
int i,zm=0,sz=0;
char s[200];
gets(s);
for(i=0;s[i];i++)
{
if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]='z')zm++;
else if(s[i]>='0'&&s[i]='9')sz++;
}
printf("共有字母%d 數字%d\n",zm,sz);
輸入n個字元統計其中數字字元、*號和字母的個數。求c語言怎麼寫
對於這個問題,不需要存到陣列弊握磨。按照如下流程即可:
1 輸入一個字元,對該字元進行判斷:
a) 如果是數字,則數字累加器加一。
b) 如果是字母,則字母累加器加一。
c)如果是換行,則結束統計(以換行為結束符。如需其他結束符,根據需要更改判斷)。
2輸出結果。
程式碼:
#include <stdio.h>
int main()
{
int c, n, i;
c=n=0;
while(1)
{
i = getchar();
if(i>='0' && i <= '9') n++;
else if((i>='a' && i <= 'z')||(i>='A' && i <= 'Z'))
c++;
else if(c=='\n') break;
}
printf("數字%d個,字母%d個\n", n,c);

return 0;
}
在c語言中怎麼統計數字字元字母個數
#include <stdio.h>
int count_letter(char *str)
{
char *p = str;
int t = 0;
開始計數
while (*p != '\0') {
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
t++;
}
p++;
}
計數完成租斗
printf("letter t:%d\n", t); 列印出英文字母總數
return t; 計數結果返回
}
int main()
{
char *str = "gkdial9-1.;J19D-=-=YdlUImf"; 例項字串
count_letter(str); 呼叫計數函式
return 0;
}
輸入一串字元,統計其中數字字元的個數
a= text1
for i= 1 to len(a)
if mid(a,i,1)>=0 and mid(a,i,1)<=9 then k=k+1
next i
print k

輸入5個字元,統計其中英文字母,數字字元和其他字元的個數用C語言表示
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char line[1024];
unsigned int nnum = 0;
unsigned int nalp = 0;
unsigned int nblk = 0;
unsigned int noth = 0;
char *p = NULL;
while (gets(line))
{
p = line;
while (*p )
{
if (*p == 32)
{
nblk ;
}
else if (*p >= 48
C語言編寫函式,統計字串中數字字元的個數

#include<stdio.h>#include<string.h>main(){ int i,j=0,k; char a[1000];長度自己根皮旁據實際情況調整 printf("請輸入一串字串:\n"); gets(a); k=strlen(a); for(i=0;i<k;i++) if('0'<=a[i]<='9') j++; printf("這串字串中數字字元有%d個!\n",j);}

求解(C語言):輸入一串字元,直到輸入一個星號(*)為止,統計(輸出)其中的字母個數和數字字元個數。
#include<stdio.h> #include<conio.h> int main() { char ch; int iCountChar = 0,iCountInt = 0; while((ch=getch()) != '*') { if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) iCountChar ++; else if(ch >= '0' && ch <= '9') iCountInt ++; printf("%c",ch); } printf("%d\n%d",iCountChar,iCountInt); return 0; }
滿意請採納
輸入若干字元,以¥結束,統計並輸出字串中非數字字元的個數,用VB語言實現
Private Sub Command1_Click()
Do
a = InputBox("")
If a = "¥" Then Exit Do
If IsNumeric(a) = True Then n = n + 1
Loop
Print n
End Sub
從鍵盤上輸入若干個字元,以 * 結束。要求統計其中字母字元、數字字元、空格符和其他字元的個數,要求用 d
using System;
using System.Collections.Generic;
using System.Text;
namespace inputstring
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("輸入一個五位數");
indexer:
string input = Console.ReadLine();
string output;
while (Convert.ToInt32(input) < 9999 || Convert.ToInt32(input) > 100000)
{
Console.WriteLine("輸入不符合提示要求請重新輸入一個五位整數:");
goto indexer;
}
char[] ch = input.ToCharArray();
foreach (char c in ch)
{
Console.Write(" ",c);
}
Console.ReadKey();
}
}
}
c++的不會這個是c#的執行成功
input:12345
output:1 2 3 4 5
c語言問題。輸入一行字元,統計數字字元(0-9),字元(a~z. A~z)及其他字元的個數
簡單:
int count=0;
for(i=0;a[i]!='\0';i++)
if(a[i]>='0'||a[i]<='9||a[i]>='a'||a[i]<='z'||a[i]>='A'||a[i]<='Z')
count++;
printf("%d\n",count); 統計的個數是:

⑤ C語言。編寫一個程序統計輸入字元的大寫字母個數m和小寫字母個數n,並輸出m,n的值

你這代碼已經實現了計算大寫字母的功能,你只要再定義一個高森變數計數小寫字母比如intn,然後在循環中添加一個小寫字母的判斷計數(n++)。

#include <stdio.h>

int main()

{

char input[256]={0};

int i=0,m=0,n=0;

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

scanf("%s",input);

while(input[i]!='')

{

臘州 if(input[i]>='A' && input[i]<='Z')

m++;

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

n++;

i++;

}

printf("大寫字母有%d個,小寫輪念蔽字母有%d個 ",m,n);

return 0;

}

⑥ c語言輸入一行字元串,如何統計其中的字母和數字的個數

要統計英文字母,空格,數字和其他字元的個數,代碼如下:

#include&lt;stdio.h&gt;

#include&lt;stdlib.h&gt;

int main()

{

char c;

int letters=0;

int space=0;

int digit=0;

int other=0;

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

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

{

if((c&gt;='a'&&c&lt;='z')||(c&gt;='A'&&c&lt;='Z'))

{

letters++;

}

else if(''==c)

{

space++;

}

else if(c&gt;='0'&&c&lt;='9')

{

digit++;

}

else

{

other++;

}

}

printf("字母的個數:&gt;%d 空格的個數:&gt;%d

數字的個數:&gt;%d 其他字元的個數:&gt;%d ",

letters,space,digit,other);

system("pause");

return 0;

}

(6)c語言統計字母個數擴展閱讀:

include用法:

#include命令預處理命令的一種,預處理命令可以將別的源代碼內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程序代碼;可以定義類似標識符功能的宏,在編譯時,預處理器會用別的文本取代該宏。

插入頭文件的內容

#include命令告訴預處理器將指定頭文件的內容插入到預處理器命令的相應位置。有兩種方式可以指定插入頭文件:

1、#include&lt;文件名&gt;

2、#include"文件名"

如果需要包含標准庫頭文件或者實現版本所提供的頭文件,應該使用第一種格式。如下例所示:

#include&lt;math.h&gt;//一些數學函數的原型,以及相關的類型和宏

如果需要包含針對程序所開發的源文件,則應該使用第二種格式。

採用#include命令所插入的文件,通常文件擴展名是.h,文件包括函數原型、宏定義和類型定義。只要使用#include命令,這些定義就可被任何源文件使用。如下例所示:

#include"myproject.h"//用在當前項目中的函數原型、類型定義和宏

你可以在#include命令中使用宏。如果使用宏,該宏的取代結果必須確保生成正確的#include命令。例1展示了這樣的#include命令。

【例1】在#include命令中的宏

#ifdef _DEBUG_

#define MY_HEADER"myProject_dbg.h"

#else

#define MY_HEADER"myProject.h"

#endif

#include MY_HEADER

當上述程序代碼進入預處理時,如果_DEBUG_宏已被定義,那麼預處理器會插入myProject_dbg.h的內容;如果還沒定義,則插入myProject.h的內容。

⑦ 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;

}

(7)c語言統計字母個數擴展閱讀:

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

⑧ C語言:任意輸入10個字元,統計英文字母的個數(包括大小寫),數字字元的個數和其它字元的個數並輸出

#include"stdio.h"
int粗畢main(intargc,char*argv[]){
chara,b,c,ch,i;
printf("Pleaseenterthe10characters... ");
for(a=b=c=i=0;i<10;i++){
scanf("中凳衫%c",&ch);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
a++;
elseif(ch>='0'&&ch<='賣腔9')
b++;
else
c++;
}
printf("Theletter:%d Thedigit:%d Theother:%d ",a,b,c);
return0;
}

運行樣例:

⑨ 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;

}

(9)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 循環可以相互轉換。

熱點內容
資料庫的根本目標 發布:2025-07-18 21:37:50 瀏覽:937
壓縮機的流速 發布:2025-07-18 21:37:40 瀏覽:406
三星怎麼取消手機密碼 發布:2025-07-18 21:33:50 瀏覽:629
安卓手機耳機如何彈窗顯示電量 發布:2025-07-18 21:20:53 瀏覽:59
雲伺服器搭建需要什麼工具 發布:2025-07-18 20:51:08 瀏覽:322
如何提高手機緩存速度 發布:2025-07-18 20:24:48 瀏覽:237
vba讀取資料庫數據 發布:2025-07-18 20:24:48 瀏覽:608
shell解壓zip 發布:2025-07-18 20:20:36 瀏覽:859
安卓泰拉瑞亞去哪裡買 發布:2025-07-18 20:01:05 瀏覽:694
flash編譯器 發布:2025-07-18 19:49:38 瀏覽:487