[Niuke.com] OR63 delete public string

1. Title description

Niuke.com question link: Delete public characters_Niuke.com title_Niuke.com

describe:

Input two strings and delete all characters in the second string from the first string. For example, if you enter "They are students." and "aeiou", the first string after deletion becomes "Thy r stdnts."

Enter description:

Each test input contains 2 strings.

Output description:

Output the deleted string.

Example 1:

Input:They are students.
aeiou

Output: Thy r stdnts.

Question details:


2. Question ideas

Idea one:

First, we create a pointer (*str) and then use this pointer to iterate through the entire arr1 array.

If it is checked that the character pointed to by the pointer belongs to arr2, the characters after *str will be advanced one byte one by one.

If it is checked that the character pointed to by the pointer does not belong to arr2, no operation is performed and the next character is checked.

Just print out arr1 after traversing and checking the last character in arr1.

The idea diagram is as follows:


 Idea two:

First of all, for online OJ questions, we can only focus on the results, that is, as long as the final printed results meet the requirements of the question.

Therefore, we can first print the contents of the first string one by one.

Then, during the printing process, it is judged whether the character belongs to the second string,

If the character belongs to the second string, the character is not printed, if the character does not belong to the second string, the character is printed.

The idea diagram is as follows:


3. Problem solving code

Idea 1 Complete problem solving code:

Based on the above ideas, the code to solve this problem is as follows:

#include <stdio.h>
#include <string.h>
void move(char*str)
{
    char*mov=str;
    while(*(mov)!='\0')
    {
        *mov=*(mov+1);//这步结束后,str指针中存储的将会是原本它后面的那个字符,因此move后str就不用再++了
        mov++;
    }
}

int main()
{
    char arr1[1000]={0};
    char arr2[1000]={0};
    gets(arr1);
    gets(arr2);
    char *str=arr1;
    char *arr1init=arr1;
    while(*str!='\0')
    {
        //判断是否为arr2字符
        if(strchr(arr2,*str)!=NULL)
        {
            //*str后所有字符前移
            move(str);
        }
        else
        //str后移
        str++;
    }
    while(*arr1init!='\0')
    {
        printf("%c",*arr1init);
        arr1init++;
    }
    return 0;
}

Copy to Niuke.com and test run:

Passed successfully:


The complete solution code for idea 2:

Based on the above ideas, the code to solve this problem is as follows:

#include <stdio.h>
#include <string.h>

int main() {
    char arr1[100]={0};
    char arr2[100]={0};
    gets(arr1);
    gets(arr2);
    int i=0;
    int len=strlen(arr1);
    for(i=0;i<len;i++)
    {
        if(strchr(arr2, arr1[i])==NULL)
            printf("%c",arr1[i]);
    }
    return 0;
}

Copy to Niuke.com and test run:

Passed successfully:


 

Guess you like

Origin blog.csdn.net/weixin_72357342/article/details/132948662