PTA: Use function to delete characters in the string (10 points) (C language)

Enter a positive integer repeat (0 <repeat <10), these times do repeat operations:

Enter a string str, and then enter a character c, c all the characters appearing in the string str deleted.

And calls the function defined in claim delchar (str, c), its function is to remove all characters in the string str c occurred, the type of the function parameter is a character pointer str, parameter c is of type char, is a function of the type of void .

Example O: described in brackets, without O

Sample input:
. 3 (REPEAT =. 3)
Happy new new year (character string "Happy new new year")
A (to be deleted character 'A')
Bee (string "Bee")
E (to be deleted character 'e')
111211 (character string "111211")
1 (a character to be deleted '1')

Output Sample:
Result: hppy new new YER (string "happy new year" in the character 'a' are removed)
Result: B (character string "bee" of the character 'e' are removed)
Result: 2 ( the string "111 211" in the character '1' are deleted)

#include <stdio.h>

void delchar(char *str, char c);

int main()
{
    int i, j, repeat;
    char c, s[100];

    scanf("%d ", &repeat);
    for (i = 1; i <= repeat; i++)
    {
        gets(s);
        //scanf("%c");
        
        scanf("%c", &c);
        //printf("%s\n", s);
        delchar(s,c);
        printf("result: %s\n", s);
    }
}

void delchar(char *str, char c)
{
    int i, j;

    i = j = 0;
    while (str[i] != '\0')
    {
        if (str[i] != c)
            str[j++] = str[i];
        i++;
    }
    str[j] = '\0';
}
Published 58 original articles · won praise 21 · views 609

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105399499