C language statistical characters
Code and Analysis for calculating the number of characters in a segment.
Post Code first:
# Include <stdio. h> # include <string. h> // provides a memset function prototype. Int main () {char S [80]; int C [26], length; // a declaration, that is, the character array S, the integer array C, and the integer length. Memset (C, 0, sizeof (C); // clears C. If you do not clear the character array, there will be an experiment later. For (int I = 0; I <= 3; I ++) // cyclically 4 times, read four lines of strings. {Gets (S); // obtain the string length = strlen (S); // obtain the string length for (int j = 0; j <length; j ++) // Loop length, length = Character Count {if (S [j]> = 'A' & S [j] <= 'Z ') // This piece of code is critical. The judgment statement and characters must be capitalized for statistics, that is, ~ Z c [S [j]-'a'] ++; // S [j]-'A' is the serial number of S [j] in the alphabet, remember, if S [j] is a number, write it as S [j]-'0 '. } For (int I = 0; I <26; I ++) printf ("% d", C [I]); // loop 26 times, output the number after statistics. Return 0 ;}
The following is an experiment. comment out the memset statement.
The result is a string of numbers to see why:
It can be seen that when Initialization is not performed, the C array is an indefinite number, because it needs to count the number of characters, so it needs to be cleared.
if(S[j]>='A'&&S[j]<='Z') C[S[j]-'A']++;
This piece of code is the essence of the program. Someone may write it for a long time to count the number of characters in each letter. Here, only two arrays are used, which utilizes the computer storage capability.
I later searched for information on the Internet and found a piece of more interesting code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int ff[26];int main(){ int i,j,n,maxn=0;char a[81]; for(i=0;i<4;i++) { gets(a); n=strlen(a); for(j=0;j<n;j++)if(a[j]>='A'&&a[j]<='Z')ff[a[j]-'A']++; } for(i=0;i<26;i++)maxn=max(maxn,ff[i]); for(i=maxn;i>0;i--){ for(j=0;j<26;j++) if(ff[j]>=i)printf("* ");else printf(" "); printf("\n");} for(i=0;i<26;i++)printf("%c ",i+'A');}
Input example
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.THIS IS AN EXAMPLE TO TEST FOR YOURHISTOGRAM PROGRAM.HELLO!
Output example
**************************************** **************************************** * *** A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Here is a max function, which is included in the header file <algorithm> in the c ++ standard library. The prototype of the min function in the c ++ 11 standard: <algorithm>:
default (1)
template <class T> const T& min (const T& a, const T& b);custom (2) template <class T, class Compare> const T& min (const T& a, const T& b, Compare comp);initializer list (3) template <class T> T min (initializer_list<T> il); template <class T, class Compare> T min (initializer_list<T> il, Compare comp);
Use them to calculate the maximum and minimum values:
maxn=max(maxn,ff[i]);
After analyzing the max function, let's analyze the essence of this Code:
if(ff[j]>=i)printf("* ");else printf(" ");
Simulate, It is output if it can be output *, otherwise skip.
Then the program result is:
PS: I write these notes in my spare time, but the purpose is not clear. Maybe it is to better understand or review the C language in the future!