c語言統計字母個數
Ⅰ c語言統計字母個數問題
思路:統計字母有兩種方式:
1.每次輸入一個字元,並判斷是否是字母,直到回車退出。
//參考代碼:
#include<stdio.h>
int main()
{
char c;
int num=0;
while((c=getchar())!='\n')
{
if(('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
num++;
}
printf("%d",num);
return 0;
}
/*
運行結果:
adf adsfasdf
11
*/
2.定義一個字元數組,一次輸入,最後遍歷該字元數組,統計字母個數。
//參考代碼
#include<stdio.h>
#include<string.h>
int main()
{
char ch[100];
gets(ch);
int num=0,i;
for(i=0;i<strlen(ch);i++)
if(('A'<=ch[i]&&ch[i]<='Z')||('a'<=ch[i]&&ch[i]<='z'))
num++;
printf("%d",num);
return 0;
}
/*
運行結果:
adf adsfasdf
11
*/
Ⅱ C語言 輸入一行字元串,統計字母,數字和其它符號的個數
可以參考下面的代碼:
#include<stdio.h>
int main()
{char s[200];
int i,zm=0,sz=0,qt=0;
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++;
else qt++;
printf("zm=%d, sz=%d, qt=%d ",zm,sz,qt);
return 0;
}
(2)c語言統計字母個數擴展閱讀:
1、for循環小括弧里第一個「;」號前為一個為不參與循環的單次表達式,其可作為某一變數的初始化賦值語句, 用來給循環控制變數賦初值; 也可用來計算其它與for循環無關但先於循環部分處理的一個表達式。
「;」號之間的條件表達式是一個關系表達式,其為循環的正式開端,當條件表達式成立時執行中間循環體。
2、IF語句三種形式
if(表達式)語句
if(表達式)語句1else語句2
if(表達式1)語句1
嵌套
在if語句中又包含一個或多個if語句稱為if語句的嵌套
Ⅲ c語言作業:輸入任意字元串,統計字母a的個數
循環遍歷一遍,是a就累加一下就好了。
#include<stdio.h>
#defineMAX100
intmain()
{
intiSum=0;//統計計數
inti;
charsStr[MAX];
gets(sStr);
for(i=0;;i++)
{
if(sStr[i]!=0)//判斷不為結尾符
{
if(sStr[i]=='a')//判斷為a字元
{
iSum+=1;
}
}
else
{
break;
}
}
printf("Numbersofchar'a'is%d",iSum);
return0;
}
Ⅳ 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 <stdio.h>
int count_letter(char *str)
{
char *p = str;
int cnt = 0;
//開始計數
while (*p != '\0') {
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
cnt++;
}
p++;
}
//計數完成
printf("letter cnt:%d\n", cnt); //列印出英文字母總數
return cnt; //計數結果返回
}
int main()
{
char *str = "gkdial9-1.;J19D-=-=YdlUImf"; //實例字元串
count_letter(str); //調用計數函數
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]!='\0';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\n",lower);
printf("upper
English
character:%d\n",upper);
printf("digit
character:%ld\n",digit);
printf("space:%d\n",space);
printf("other
character:
%ld\n",others);
return
0;
}
int
main()
{
while(1)
{
func3();
printf("\n");
system("pause");
}
return
0;
}
(7)c語言統計字母個數擴展閱讀:
程序實現思路分析
統計大小寫字母、數字的個數,首先要判斷出字元是屬於哪一種,然後增加計數。
1、判斷
小寫字母的范圍為:'a'~'z'
大寫字母的范圍為:'A'~'Z'
數字的范圍為:'0'~'9'
2、聲明三個int變數並賦值初值為0
lower——統計小寫英文字母
upper——統計大寫英文字母
digit——統計數字
Ⅷ 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;
}
(8)c語言統計字母個數擴展閱讀:
程序思路:
分為三部分 首先輸入字元串 ,其次設定一個字元數組英文小寫字母24, 同時設一個int數組 記錄個數, 以及一個int c 為了給int數組賦值。最後在輸入的時候進行判斷,如果字母的值 大於等於1才輸出。
Ⅸ C語言編寫一個函數統計大寫字母個數
#include <stdio.h>
int main(){ char input[256]={0};//存儲輸入要足夠大 int i=0,count=0; printf("請輸入字元串:\n"); scanf("%s",input); while(input[i]!='\0') { if(input[i]>='A' && input[i]<='Z') { count++; } i++; } printf("大寫字母有%d個\n",count); return 0;}