(C syntax FAQ 22) statistical characters, syntax 22
Knowledge point: the use of arrays, combined with loops
Getchar () Usage
Note: For array a [1000], there is no a = getchar () and then the characters are loaded into the array one by one. This is incorrect.
And scanf ("% s", a); is not correct.
Content: enter several characters (ending with '#') to calculate the number of times 'A' or 'A' appears in the entered characters.
Input description:
A row contains several characters.
Output description:
An integer that represents the number of occurrences of the or A character and.
Input example: abcdefABCDEFaaAA #
Output example: 6
1 #include <stdio.h> 2 int main() 3 { 4 char a[500]; 5 int i,s=0; 6 for (i=0;i<500;i++) 7 { 8 a[i]=getchar(); 9 if(a[i]==65||a[i]==97)10 {11 s+=1;12 }13 if(a[i]==35)14 {15 break;16 }17 }18 printf("%d\n",s);19 return 0;20 }
Count the number of characters in a string C
# Include <stdio. h>
# Include <stdlib. h>
Void main ()
{
Char s [200];
Int m [200] [2];
Int I, j, k, n, l;
Printf ("please enter a string \ n ");
Scanf ("% s", s); // enter a blank string using gets, and use scanf if no blank string exists.
L = strlen (s );
N = 1;
M [n] [0] = s [0];
M [n] [1] = 1;
For (I = 1; I <= l; I ++ ){
For (j = 1; j <= n; j ++)
{
If (s [I] = (m [j] [0] & 0xff) {m [j] [1] ++; goto Lab ;};
};
N ++;
M [n] [0] = s [I];
M [n] [1] = 1;
Lab :;
}
// If You Want To sort by ASCII value, do the following:
For (I = 1; I <n-1; I ++)
For (j = I + 1; j <n; j ++)
If (m [I] [0]> m [j] [0]) {
K = m [I] [0]; m [I] [0] = m [j] [0]; m [j] [0] = k;
K = m [I] [1]; m [I] [1] = m [j] [1]; m [j] [1] = k;
}
For (I = 1; I <n; I ++) printf ("% c = % d", m [I] [0], m [I] [1]);
Exit (0 );
}
C. Count characters
# Include <string. h>
# Include <stdio. h>
Void fun (char s [], int num [])/* comma */
{Int I;
For (I = 0; I <strlen (s); I ++)
{If (s [I] <= '9' & s [I]> = '0 ')
Num [0] ++;
If (s [I] <= 'Z' & s [I]> = 'A ')
Num [1] ++;
If (s [I] <= 'Z' & s [I]> = 'A ')
Num [2] ++;
If (s [I] = '')
Num [3] ++;
}
Printf ("Number Large letter Small letter Null \ n ");
For (I = 0; I <4; I ++)
Printf ("% 11d", num [I]);
}
Main ()
{Int num [4] = {0, 0, 0}; char s [200];/* specify the array length, defined in the main function */
Gets (s );
Fun (s, num);/* No length required for pointer call */
}
I can run