當前位置:首頁 » 編程語言 » c語言分辨字元串數字和字母個數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言分辨字元串數字和字母個數

發布時間: 2022-06-09 05:32:53

⑴ 用c語言如何判斷一個字元串常量的字母個數,數字個數,空格個數,其他個數

下面是我的程序:
#include

#define
n
81
#include

int
main()
{
char
str[n];
int
i,n,zimu,num,kongge,others;
zimu=num=kongge=others=0;
gets(str);
n=strlen(str);
for(i=0;i
='a'&&str[i]<='z')
zimu++;
else
if(str[i]>='a'&&str[i]<='z')
zimu++;
else
if(str[i]>='0'&&str[i]<='9')
num++;
else
if(str[i]='
')
kongge++;
else
others++;
printf("\n字母:%d,數字:%d,空格:%d,其他:%d",zimu,num,kongge,others);
return
0;
}
希望對你有所幫,記得採納哦。

⑵ C語言中如何識別字元與數字

你直接將每個字元挨個提取出來,將他賦給一個整型,c會自動將該字元轉化為asc碼值的。然後根據整型在去判斷它到底是數字還是字元還是空格,空格有特定的asc碼值

⑶ c語言 輸入一個字元,判斷是字母,數字,還是特殊字元

#include <stdio.h>

#include<string.h>

#define N 100

main()

{

char all[N];

int i;

printf("請輸入一個字元串(不超過100個):");

gets(all);

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

{

if(all[i]>'a'&&all[i]<'z'||all[i]>'A'&&all[i]<'Z')

printf("字母! ");

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

printf("數字! ");

else printf("其他字元! "); }

}

(3)c語言分辨字元串數字和字母個數擴展閱讀:

根據ascii碼值判斷即可。由於數字,大小寫字母均分別為連續存儲,所以只需要與對應的最大最小值比較即可確定字元類型。、

⑷ C語言 判斷字元串中大寫小寫個數和數字個數

統計字元串中大寫小寫個數和數字個數,過程:

  • 定義三個變數u,l,d,並初始化為0

  • 遍歷字元串,循環檢查每一個字元,

    • 如果是大寫字母,則累加u

    • 如果是小寫字母,則累加l

    • 如果是數字,則累加d

    • 如果是字元串結束符,則結束循環

  • 循環結束,輸出統計結果。

參考代碼:

#include<stdio.h>
#definenum1000
intmain()
{
intu=0,l=0,d=0;
inti;
chararray[num];
printf("請輸入一段文章: ");
gets(array);

for(i=0;array[i]!='';i++)//輸入的字元不一定是num個,所以,只需要檢查到字元串結束符就可以了
{
if('A'<=array[i]&&array[i]<='Z')u++;
elseif('a'<=array[i]&&array[i]<='z')l++;
elseif('0'<=array[i]&&array[i]<='9')d++;
}
printf("大寫:%d 小寫:%d 數字:%d ",u,l,d);
return0;
}

運行:

請輸入一段文章:

helloworld2015-ChinaUnix博客

大寫:2

小寫:17

數字:4

Press any key to continue

⑸ c語言 從鍵盤輸入一行字元,分別統計其中數字字元,字母字元和其他字元的個數

可以參考下面的代碼:

#include <stdio.h>

intmain()

{

inta,b,c,ch;

a=b=c=0;//計數器初始化為0.

while((ch=getchar())!=' ')//循環讀取字元,到換行結束。

{

if(ch>='0' && ch<='9')//數字

a++;

else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))//字母

b++;

else//其它

c++;

}

printf("%d%d%d ",a,b,c);//輸出結果。

return0;

}

(5)c語言分辨字元串數字和字母個數擴展閱讀:

printf()函數函數

printf()函數是格式化輸出函數, 一般用於向標准輸出設備按規定格式輸出信息。在編寫程序時經常會用到此函數。函數的原型為:

int printf(const char *format, ...);

函數返回值為整型。若成功則返回輸出的字元數,輸出出錯則返回負值,printf()函數的調用格式為:

printf("<格式化字元串>", <參量表>);

while語句的一般表達式為:while(表達式){循環體}。

⑹ c語言統計字元串大寫字母和數字個數,如圖

簡單寫一下public static void main(String args[]){
int bigNum = 0;
int smallNum = 0 ;
String str="asdakshf KDH SKDK SBD KSNm,d dsjbsjcns\ndc"; //要判斷的字元串 int index ; //回車鍵的位置
if(str.indexOf("\n")==-1){ //沒有回車鍵時
index = str.length() ;
}else{
index = str.indexOf("\n");
}
String newStr = str.substring(0,index ) ; for(int i=0;i<newStr .length();i++){
char c = newStr .charAt(i);
if(c>=65&&c<=90){
bigNum++;
}else if(c>=97&&c<=122){
smallNum++ ;
}
}
System.out.println("大寫字母個數:"+bigNum+",小寫字母個數:"+smallNum); System.out.println(" 總字元個數:"+str.length());
}

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

}

(7)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語言統計字元串中字母、數字的個數 以下是我的代碼。

#
include
int
i,z=0,s=0,q=0;
void
f1(char
str[100],char
str1[100],char
str2[100],char
str3[100]);
//全局變數
int
main
()
{
char
str[100],str1[100],str2[100],str3[100];
printf
("input
string:\n");
gets(str);
f1(str,str1,str2,str3);
printf
("字母數:
%d
%s\n",z,str1);
printf
("數字個數:%d
%s\n",s,str2);
printf
("其他字元:%d
%s\n",q,str3);
return
0;
}
void
f1(char
str[100],char
str1[100],char
str2[100],char
str3[100])
{
for
(i=0;
str[i]
!=
'\0';
i++)
if
((str[i]
>=
'A')
&&
(str[i]
<=
'Z')
||
(str[i]
>=
'a')
&&
(str[i]
<=
'z'))
{
str1[z++]
=
str[i];
}
else
if
(str[i]
>=
'0'
&&
str[i]
<=
'9')
{
str2[s++]
=
str[i];
}
else
{
str3[q++]
=
str[i];
}
str1[z]
=
'\0';
str2[s]
=
'\0';
str3[q]
=
'\0';
//全放在這里
}

⑼ c語言輸入一個字元串判斷有多少大小寫字母,數字和空格這個

只要不是關鍵字,數字開頭,只要是以字母和下劃線開頭,並且後續字元都是數字或者字母或者下劃線多長都可以。但是不宜太長,否則可讀性差。比如
int
_sjoifjsflajfkldsjflasjflasfjk;肯定可以,但是太長,可讀性不好。