(C syntax FAQ 22) statistical characters, syntax 22

Source: Internet
Author: User

(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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.