String statistics, count the number of characters in the string
1 # include <stdio. h> 2 # include <string. h> 3 4 // define the substring parameter struct 5 struct sub_str 6 {7 char sstr [61]; // The substring 8 int len; // The substring length 9 int times; // occurrence times of the substring: 10}; 11 12 int main () 13 {14 int I, j, k; 15 int l, l_temp; // l: minimum length of the substring to be counted for record input; l_temp: used to record the temporary dynamic substring length when traversing the substring 16 char str [61], sstr_temp [61]; // str: indicates the input string to be counted; sstr_temp: indicates the input string 17 int str_len; // The length of the string to be counted for record input 18 struct sub_str substr [1830]; // substr [1830]: used to record parameters of each substring, A maximum of 1 + 2 + 3 + · + 60 sub-strings 19 struct sub_str substr_temp; // substr_temp: the temporary intermediate struct variable 20 int sstr_num = 0 used to sort the substrings in substr [1830] in the Exchange Order; // used to count the actual number of substrings stored in substr [1830], which is 21 int max_times; // used to record the maximum number of times a substring appears 22 23 // enter 24 scanf ("% d", & l ); // The minimum length of the substring for input statistics is 25 getchar (); // remove the Input key 26 gets (str ); // enter the string to be counted 27 28 str_len = strlen (str ); // obtain the length of the input string to be counted: 29 30 // The initial length of the temporary dynamic substring is the maximum length, traverse the substring 31 for (l_temp = str_len; l_temp> = l; l_temp --) 32 {33 for (I = 0; I <str_len-l_temp; I ++) // The substring whose length is l_temp has a str_len-l_temp of 34 {35 for (j = I, k = 0; j <I + l_temp; j ++, k ++) // obtain the substring 36 sstr_temp [k] = str [j]; 37 38 for (k = 0; k <sstr_num; k ++) // query whether the substring 39 if (strcmp (sstr_temp, substr [k] to be counted exists in substr [1830]. sstr) = 0) // If yes, the occurrence times of the substring are increased by 1 40 {41 substr [k]. times ++; 42 break; 43} 44 45 if (k = sstr_num) // if no, this substring is added to substr [1830, and initialize each parameter 46 {47 strcpy (substr [sstr_num]. sstr, sstr_temp); 48 substr [sstr_num]. len = l_temp; 49 substr [sstr_num]. times = 1; 50 sstr_num ++; // the actual number of substrings in substr [1830] plus 1 51} 52 53 memset (sstr_temp, 0, sizeof (sstr_temp )); // clear 0 sstr_temp [61], continue to traverse the length of the sub-string 54} 55} 56 57 // sort the sub-strings in substr [1830] by bubble sort method 58 for (I = 0; I <sstr_num-1; I ++) 59 {60 for (j = I; j <sstr_num; j ++) 61 {62 if (substr [j]. times> substr [I]. times) 63 {64 for (k = 0; k <61; k ++) 65 substr_temp.sstr [k] = substr [I]. sstr [k]; 66 substr_temp.len = substr [I]. len; 67 substr_temp.times = substr [I]. times; 68 69 for (k = 0; k <61; k ++) 70 substr [I]. sstr [k] = substr [j]. sstr [k]; 71 substr [I]. len = substr [j]. len; 72 substr [I]. times = substr [j]. times; 73 74 for (k = 0; k <61; k ++) 75 substr [j]. sstr [k] = substr_temp.sstr [k]; 76 substr [j]. len = substr_temp.len; 77 substr [j]. times = substr_temp.times; 78} 79} 80} 81 82 // output 83 puts (substr [0]. sstr); 84 85 return 0; 86}