Descriptionapi
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.less
Input函數
first line is a number n(0<n<=50), which stands for the number of test data.this
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.spa
Output 指針
string A after delete characters, and each output is split by "\n"blog
if string A is null after delete, you just need to print "\n"ip
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.
個人
#include<stdio.h> #include<string.h> int main() { int n, jud = 1, i, j; char a[50], b[50]; scanf("%d", &n); getchar();//這個不要忘記,否則會錯誤的 while (n--) { while (jud) { fgets(a, 50, stdin);//這裏用gets!!!一開始用gets,怎麼弄都弄不對,因此用了fgets fgets(b, 50, stdin); jud--; } jud = 1; int a1 = strlen(a), b1 = strlen(b); for (i = 0; i < a1; i++) { for (j = 0; j < b1; j++) { if (a[i] == b[j]) { a[i] = '1'; break; } } } for (i = 0; i < a1; i++) { if (a[i] != '1') { printf("%c", a[i]); } } printf("\n"); } return 0; }
標程
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.}
①gets——從標準輸入接收一串字符,遇到'\n'時結束,但不接收'\n',把 '\n'留存輸入緩衝區;把接收的一串字符存儲在形式參數指針指向的空間,並在最後自動添加一個'\0'。
getchar——從標準輸入接收一個字符返回,多餘的字符所有留在輸入緩衝區。
fgets——從文件或標準輸入接收一串字符,遇到'\n'時結束,把'\n'也做爲一個字符接收;把接收的一串字符存儲在形式參數指針指向的空間,並在'\n'後再自動添加一個'\0'。
簡單說,gets是接收一個不以'\n'結尾的字符串,getchar是接收任何一個字符(包括'\n'),fgets是接收一個以'\n'結尾的字符串。
scanf( )函數和gets( )函數均可用於輸入字符串,但在功能上有區別。
gets能夠接收空格
scanf遇到空格、回車和Tab鍵都會認爲輸入結束,全部它不能接收空格
fgets用法:
fgets(buf,sizeof(s),stdin):
fgets(buf, n, file) 函數功能:從 目標文件流 file 中讀取 n-1 個字符,放入以 buf 起始地址的內存空間中。
樓主的函數調用是這個意思:
首先,s 確定是一個字符數組。
該調用從 標準輸入流 stdin (也就是鍵盤輸入)讀入 s 數組的大小(sizeof(s))再減 1 的長度的字符到 buf 所指的內存空間中(前提是buf已經申請好空間了)
在c語言小知識裏面有相關資料~~