判断字符串2中的字符是否全部在字符串1中出现
1 #include <iostream> 2 using namespace std; 3 4 5 //判断字符串2中的字符是否全部在字符串1中出现 6 bool same(char*str1, char*str2) 7 { 8 int hashtable_str1[256] = { 0 }; 9 int hashtable_str2[256] = { 0 }; 10 11 12 for (int i = 0; i < strlen(str1 - 1); i++) //用表1记录字符串1的字符 13 { 14 hashtable_str1[str1[i]] = 1; //出现则为1,没出现则为0 15 } 16 17 18 for (int i = 0; i < strlen(str2 - 1); i++) //表2记录字符串2的字符 19 { 20 hashtable_str2[str2[i]] = 1; 21 } 22 23 for (int i = 0; i < 256; i++) //比较表1和表2 24 { 25 if (hashtable_str1[i] < hashtable_str2[i]) return false; 26 } 27 return true; 28 } 29 int main() 30 { 31 char string1[50]; 32 char string2[50]; 33 34 cout << "string1:"; 35 cin >> string1; 36 cout << "string2"; 37 cin >> string2; 38 39 int result=same(string1, string2); 40 if (result == 0) 41 cout << "no"; 42 else 43 cout << "yes"; 44 system("pause"); 45 }