c語言字元計數
1. c語言中統計字元串中各個字元的個數
原發布者:zlaikai1314
#include#include#include#includeusingnamespacestd;chara[100];//字元數組intb[100];//字元個數doublep_a[100];//字元概率數組intsum=0;//字元總數//判斷當前字元temp是否已出現過boolsearch(chartemp,chara[],intm,intn)//m為數組a的元素總個數,即100;n為當前數組a中存放的字元種類的個數{inti=0;while(i<n){if(a[i]==temp)returntrue;elsei++;}returnfalse;}//求各個字元的個數,放在數組b中voidread_file(stringfile_name="test_data.txt"){intk=0;ifstreamfile(file_name.c_str());//將string轉化為char數組chartemp;if(file.is_open()==true)//檢查文件是否打開{while(file.peek()!=EOF)//從文件中讀取一個字元,但該字元並未從輸入流中刪除{file.get(temp);//從文件讀入一個字元並把它存儲在tempsum++;//統計出現的字元總數if(search(temp,a,100,k)){for(inti=0;i<k;i++){if(temp==a[i]){b[i]++;break;}}}else
2. c語言 統計字元個數
要統計英文字母,空格,數字和其他字元的個數,代碼如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf("請輸入一行字元:>");
while((c=getchar())!='\n')
{
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\n空格的個數:>%d\
\n數字的個數:>%d\n其他字元的個數:>%d\n",\
letters,space,digit,other);
system("pause");
return 0;
}
3. C語言字元統計
#include<stdio.h>
intmain()
{inti,a,n=0;
charc,s[101];
scanf("%c%d%*c",&c,&a);
gets(s);
if(a==0&&c>='a'&&c<='z')c-=32;
for(i=0;s[i];i++)
if(c==(a==0&&s[i]>='a'&&s[i]<='z'?s[i]-32:s[i]))n++;
printf("%d
",n);
return0;
}
4. 字元計數 c語言
統計輸入的字元總數
比如輸入aaa^Z可以列印出總數3
之所以沒看到列印是因為窗口被關閉了
可以在結尾printf後加上
system("pause");
#include
main()
{
long
nc;
nc=0;
while(getchar=())
!=EOF)
++nc;
printf("%d\n",nc);
system("pause");
}
5. C語言字元串計數
#include<stdio.h>
intmain(void)
{
charaChar;
intspaces=0,upper=0,lower=0,other=0;
do{
scanf("%c",&aChar);//讀取字元
if(aChar=='')//判斷是否空格
{
++spaces;
continue;
}
if('A'<=aChar&&aChar<='Z')//判斷是否大寫字母
{
++upper;
continue;
}
if('a'<=aChar&&aChar<='z')//判斷是否小寫字母
{
++lower;
continue;
}
++other;//以上都不是,則是其他字元
}while(aChar!=' ');
printf("spaces=%d,upper=%d,lower=%d,other=%d ",
spaces,upper,lower,other-1);
return0;
}
因為是用' '來判斷一行字元串的,而 也是一個字元,導致最後統計other會多1個,所以要other-1,也可以加一個if語句來判斷:
if(aChar!=' ')
++other;
當然,就算把
也統計進去,也是符合題意的。
6. c語言怎麼計算字元串的字元個數
一般有三種辦法可以計算英文字元的個數:
1)使用strlen()函數
2)從首字元開始,邊掃描邊計數,到'\0'為止('\0'不計數)
3)從首字元開始,掃描到'\0'為止,'\0'地址與字元串首地址的差。
7. c語言如何統計字元個數
在C語言中,要統計一個字元串的字元個數,可以採用char類型的字元數組,再進行逐個位元組的掃描,如果它的ASCII值大於零,這個位元組算一個字元;如果它的ASCII值小於零的,就連同後續的一個位元組算一個字元。遇到ASCII值等於零,就停止統計輸出統計的結果。
8. C語言 字元計數
EOF是判斷讀取文件是否是結尾,但是你現在這個不是讀取文件,所以,這個應該是個死循環,最好是換成getchar!='\n';一般輸入完成後是回車所以判斷它是不是輸入回車了,輸入回車了,那麼就是輸入結束了,然後nc就是你輸入的內容的字元個數while就是個循環,
9. C語言中怎麼統計字元個數
可以通過switch語句,把字母列出來,然後每個字母設置一個變數,遍歷數組,對不同字母的變數每次遇到加加就行了