C語言學習2.2021/2/6
阿新 • • 發佈:2021-02-07
譚浩強C語言第八章指標8.輸入一行文字,找出其中大寫字母,小寫字母,數字,空格,其它字元的數量
#include<stdio.h>
int main()
{
char s[50], *p;
int small = 0, big = 0, table = 0, number = 0, other = 0;
printf("please enter an string:\n");
gets(s);
p = s;//指標p指向字串s
while (*p != '\0')
{
if ((*p >= 'A')&&( *p <= 'Z'))
big++;
else if (*p >= 'a'&&*p <= 'z')
small++;
else if (*p == ' ')
table++;
else if (*p >= '0'&&*p <= '9')
number++;
else
other++;
p++;
}
printf("字串中:\n大寫字母個數:%d\n小寫字母個數:%d\n空格個數:%d\n數字個數:%d\n其它字元個數:%d\n", big, small, table, number, other);
return 0;
}
寫的時候把判斷條件寫成了*p!=’\n’,所以結果報錯,後來改過來了。
執行結果: