• (eden)Delete character


    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 " "

    if string A is null after delete, you just need to print " "

    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("
    ");
    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='';
    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("
    ");
    42.                        continue;
    43.                }
    44.                Delete(first, second);
    45.                printf("%s
    ", first);
    46.        }
    47.        return 0;
    48.}
  • 相关阅读:
    Linux下运行当前目录需要加./的原因
    Linux mint界面过小无法安装(解决方法)
    哈工大机考:数组逆置
    哈工大机考:字符串内排序
    哈工大机考:求最大值
    八皇后问题的简单分析
    哈工大机考:字符串去特定字符
    哈工大机考:计算两个矩阵的乘积
    iOS 字号转换问题
    iOS 十六进制的颜色值转换为UIColor
  • 原文地址:https://www.cnblogs.com/iamxiaoyubei/p/5122641.html
Copyright © 2020-2023  润新知