Description:
Description
In this exercise, you will get two strings A and B in each test group and the length of every string is less than 40, you need to delete all characters which are contained in string B from string A.
The character may be space or letter.
Input
first line is a number n(0<n<=50), which stands for the number of test data.
the next 2*n lines contain 2*n strings, and each group of test data contain two strings A and B. There may be space in both A and B.
Output
string A after delete characters, and each output is split by "\n"
if string A is null after delete, you just need to print "\n"
Sample Input
3
WE are family
aeiou
qwert
asdfg
hello world
e l
Sample Output
WE r fmly
qwert
howrd
Hint:
the string contains space, so you may need to use fgets() to input a string.
Capital letter and small letter cannot be ignored.
代码
1 #include<stdio.h> 2 #include<string.h> 3 int main() { 4 int n, j, i; 5 scanf("%d", &n); 6 getchar(); 7 while (n--) { 8 char ch[50] = "0", result[50] = "0", ch1[50] = "0"; 9 char t; 10 fgets(ch, 50, stdin); 11 int k = strlen(ch); 12 fgets(ch1, 50, stdin); 13 int k2 = strlen(ch1); 14 int tot = 0; 15 for (i = 0; i < k + 1; i++) { 16 int b = 0; 17 for (j = 0; j < k2 + 1; j++) { 18 if (ch1[j] == ch[i]) { 19 b = 1; 20 } 21 } 22 if (b == 0) { 23 result[++tot] = ch[i]; 24 } 25 } 26 for (i = 1; i <= tot; i++) { 27 printf("%c", result[i]); 28 } 29 printf("\n"); 30 } 31 }
标答
1.#include<stdio.h> 2.#include<string.h> 3. 4.#define NUMBER 256 5. 6.void Delete(char *first, char *second) { 7. int i; 8. int hashtable[NUMBER]; 9. for (i = 0; i < NUMBER; i++) 10. hashtable[i]=0; 11. 12. char *p = second; 13. while (*p) { 14. hashtable[*p]=1; 15. p++; 16. } 17. 18. char *slow = first; 19. char *fast = first; 20. while (*fast) { 21. if (hashtable[*fast] == 0) { 22. *slow=*fast; 23. slow++; 24. } 25. fast++; 26. } 27. *slow='\0'; 28.} 29. 30.int main() { 31. int num; 32. char temp[50]; 33. scanf("%d", &num); 34. fgets(temp, 50, stdin); 35. while (num--) { 36. char first[50]; 37. char second[50]; 38. fgets(first, 50, stdin); 39. fgets(second, 50, stdin); 40. if (first == NULL) { 41. printf("\n"); 42. continue; 43. } 44. Delete(first, second); 45. printf("%s\n", first); 46. } 47. return 0; 48.}