本文目錄一覽:
用c語言如何實現,統計從鍵盤輸入數字的個數
可以用一個for循環,將上限設置大一點,在循環里加入if判斷跳出循環的條件,每次循環計數+1或者等全部數字輸入完成之後,直接取字符串長度
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;
}
擴展資料:
程序實現思路分析
統計大小寫字母、數字的個數,首先要判斷出字符是屬於哪一種,然後增加計數。
1、判斷
小寫字母的範圍為:’a’~’z’
大寫字母的範圍為:’A’~’Z’
數字的範圍為:’0’~’9′
2、聲明三個int變量並賦值初值為0
lower——統計小寫英文字母
upper——統計大寫英文字母
digit——統計數字
c語言輸入一行字符串,如何統計其中的字母和數字的個數
要統計英文字母,空格,數字和其他字符的個數,代碼如下:
#includelt;stdio.hgt;
#includelt;stdlib.hgt;
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf(“請輸入一行字符:gt;”);
while((c=getchar())!=’\n’)
{
if((cgt;=’a’clt;=’z’)||(cgt;=’A’clt;=’Z’))
{
letters++;
}
else if(”==c)
{
space++;
}
else if(cgt;=’0’clt;=’9′)
{
digit++;
}
else
{
other++;
}
}
printf(“字母的個數:gt;%d\n空格的個數:gt;%d\
\n數字的個數:gt;%d\n其他字符的個數:gt;%d\n”,\
letters,space,digit,other);
system(“pause”);
return 0;
}
擴展資料:
include用法:
#include命令預處理命令的一種,預處理命令可以將別的源代碼內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程序代碼;可以定義類似標識符功能的宏,在編譯時,預處理器會用別的文本取代該宏。
插入頭文件的內容
#include命令告訴預處理器將指定頭文件的內容插入到預處理器命令的相應位置。有兩種方式可以指定插入頭文件:
1、#includelt;文件名gt;
2、#include”文件名”
如果需要包含標準庫頭文件或者實現版本所提供的頭文件,應該使用第一種格式。如下例所示:
#includelt;math.hgt;//一些數學函數的原型,以及相關的類型和宏
如果需要包含針對程序所開發的源文件,則應該使用第二種格式。
採用#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的內容。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246776.html