删除相同字符

删除两个字符串数组中相同的字符
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值