2015 public comments on campus recruitment pen questions-compare the same and different parts of the two strings, 2015 campus recruitment
Implementation using an array hash table </span>
// String str1, string str2, str1 length n1, str2 length n2, res1 storage same character, res2 storage different character void charcmp (char * str1, char * str2, int n1, int n2, char * res1, char * res2) {int a1 [60] = {0}; // use a hash array to record the number of occurrences of str1 characters, there are a total of 52 uppercase and lowercase letters. The ascii alphabet contains 6 Other symbols: int a2 [60] = {0}; // records the number of occurrences of str2 characters using a hash array, there are a total of 52 uppercase and lowercase letters. The ascii alphabet contains 6 Other symbols for (int I = 0; I <n1; I ++) {a1 [str1 [I]-'a'] ++; // use str [0] to record the number of times of A} for (int I = 0; I <n2; I ++) {a2 [str2 [I]-'a'] ++;} int k = 0; int m1 = 0; int m2 = 0; for (; k <60; k ++) {if (a1 [k]! = 0 | a2 [k]! = 0) {if (a1 [k]! = 0 & a2 [k]! = 0) // The letter {res1 [m1 ++] = k + 'A' that appears in both strings; // The number is converted to A character, K = 0 indicates the number of times A is stored, corresponds to character A} else // only the characters in the string {res2 [m2 ++] = k + 'A ';}}} res1 [m1] = '\ 0'; // Add the terminator res2 [m2] =' \ 0'; // Add the terminator} int main () {char str1 [] = "abcd"; char str2 [] = "fagh"; char res1 [5] = {'\ 0 '}; char res2 [10] = {'\ 0'}; charcmp (str1, str2, 4, res1, res2); getchar (); return 1 ;}