删除两个字符串数组中相同的字符
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 (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
fgets函数的原型
char *fgets(char *buf, int bufsize, FILE *stream);
*buf: 字符型指针,指向用来存储所得数据的地址。
bufsize: 整型数据,指明存储数据的大小。
*stream: 文件结构体指针,将要读取的文件流
fgets与gets相比使用这个好处是:读取指定大小的数据,避免gets函数从stdin接收字符串而不检查它所复制的缓存的容积导致的缓存溢出问题
从文件结构体指针stream中读取数据,每次读取一行
例:
如果一个文件的当前位置的文本如下
Love, I Have
Since you can do it.
如果用fgets(str1,6,file1);去读取
则执行后str1 = “Love,” ,读取了6-1=5个字符
这个时候再执行fgets(str1,20,file1)则执行后str1 = ” I Have\n”
而如果
fgets(str1,23,file1);
则执行str1=”Love ,I Have”,读取了一行(包括行尾的’\n’,并自动加上字符串结束符’\0’),当前文件位置移至下一行,虽然23大于当前行上字符总和,可是不会继续到下一行。而下一次调用fgets()继续读取的时候是从下一行开始读。
#include<string.h>
#include<stdio.h>
int main() {
int n, k;
scanf("%d", &n);
getchar(); // 删除\n
char line1[50];
char line2[50];
for (k = 0; k < n; k++) {
fgets(line1, 50, stdin);
fgets(line2, 50, stdin);
long l1 = strlen(line1);
long l2 = strlen(line2);
for (int i = 0; i < l1; i++) {
for (int j = 0; j < l2; j++) {
if (line1[i] == line2[j]) {
line1[i] = '!';
}
}
}
for (int i = 0; i < l1; i++) {
if (line1[i] != '!') {
printf("%c", line1[i]);
}
}
printf("\n");
}
return 0;
}
标程我看不懂
#include<stdio.h>
#include<string.h>
#define NUMBER 256
void Delete(char *first, char *second) {
int i;
int hashtable[NUMBER];
for (i = 0; i < NUMBER; i++)
hashtable[i]=0;
char *p = second;
while (*p) {
hashtable[*p]=1;
p++;
}
char *slow = first;
char *fast = first;
while (*fast) {
if (hashtable[*fast] == 0) {
*slow=*fast;
slow++;
}
fast++;
}
*slow='\0';
}
int main() {
int num;
char temp[50];
scanf("%d", &num);
fgets(temp, 50, stdin);
while (num--) {
char first[50];
char second[50];
fgets(first, 50, stdin);
fgets(second, 50, stdin);
if (first == NULL) {
printf("\n");
continue;
}
Delete(first, second);
printf("%s\n", first);
}
return 0;
}