本文目錄一覽:
- 1、c語言輸入數據統計數字個數
- 2、C語言統計
- 3、c語言統計每個字母個數
- 4、c語言如何統計字元個數
- 5、分類統計字元 C語言
- 6、數值統計 C語言!!!
c語言輸入數據統計數字個數
這個簡單,只要變數字元串,判斷每個字元是否是數字(str[i]=’0′
str[i]
=9就是數字)。如果當前位不是數字,但前一個字元是數字,就說明前面有過一個數字了(數字計數+1).
//比如:
#includestdio.h
int main()
{
in i,cnt=0;
char str[11]=”ab12cd34dd”;
for(i=1;i11;i++)
if((str[i]’0′ || str[i]’9′) (str[i-1]=’0′ str[i-1] =9))
cnt++;
printf(“個數%d\n”cnt);
return 0;
}
注意保存字元串的數組,多定義一位,最後一個元素必須是0(也就是結束符合『\0』);比如上面
我寫的常量”ab12cd34dd”,只有10個字元,實際內存中有11個字元,最後一個字元就是『\0』,
所以數組我定義11位,循環也是11位
C語言統計
給:
#includestdio.h
void main()
{
int letters=0,space=0,digit=0,other=0;
char c;
while((c=getchar())!=’\n’)
{
if(c=’a’c=’z’||c=’A’c=’Z’)
letters ++;
else if(c=0c=9)
digit++;
else if(c==’ ‘)
space++;
else
other++;
}
printf(“letters=%d space=%d digit=%d other=%d\n”,letters,space,digit,other);
}
c語言統計每個字母個數
思路:統計字母有兩種方式:
1.每次輸入一個字元,並判斷是否是字母,直到回車退出。
//參考代碼:
#include
int main()
{
char c;
int num=0;
while((c=getchar())!=’\n’)
{
if((‘a’=cc=’z’)||(‘a’=cc=’z’))
num++;
}
printf(“%d”,num);
return 0;
}
/*
運行結果:
adf adsfasdf
11
*/2.定義一個字元數組,一次輸入,最後遍歷該字元數組,統計字母個數。
//參考代碼
#include
#include
int main()
{
char ch[100];
gets(ch);
int num=0,i;
for(i=0;i
評論
載入更多
c語言如何統計字元個數
在C語言中,要統計一個字元串的字元個數,可以採用char類型的字元數組,再進行逐個位元組的掃描,如果它的ASCII值大於零,這個位元組算一個字元;如果它的ASCII值小於零的,就連同後續的一個位元組算一個字元。遇到ASCII值等於零,就停止統計輸出統計的結果。
分類統計字元 C語言
例:使用while語句循環統計 :
#includestdio.h
int main()
{
char c;
int letters_num = 0, space_num = 0, digit_num = 0, other_num = 0;
while ((c = getchar()) != ‘\n’)//輸入換行符退出循環
{
if ((c = ‘a’c = ‘z’) || (c = ‘A’c = ‘Z’)) letters_num++;
else if (c == ‘ ‘) space_num++;
else if (c = ‘0’c = ‘9’) digit_num++;
else other_num++;
}
printf(“字母=%d,空格=%d,數字=%d,其他=%d”, letters_num, space_num, digit_num, other_num);
return 0;
}
運行效果:
擴展資料:
printf()函數的用法
1、printf()函數的調用格式為:printf(“lt;格式化字元串gt;”,lt;參量表gt;);
//__stdcall
int __cdecl printf(const char*p,…);
可變參數
printf在列印浮點數,不論原來是雙精度還是單精度,都變為雙精度(8位元組)
列印1位元組(char)2位元組(short)4位元組(int)==gt;4位元組,除了long long(8位元組)
void main()
{
數值統計 C語言!!!
#include stdio.h
int main()
{int n,z,f,l;float x;
scanf(“%d”,n);
while(n0)
{for(z=f=l=0;n–;)
{scanf(“%f”,x);
if(x0)z++;
else if(x0)f++;
else l++;
}
printf(“%d %d %d\n”,f,l,z);
scanf(“%d”,n);
}
return 0;
}
原創文章,作者:HTBHH,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/317416.html