Delete characters

时间:2023-03-09 19:18:13
Delete characters

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.

我的

#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语言小知识里面有相关资料~~