Character deletion

Problem description:
Write a program, first enter a string str (the length does not exceed 20), and then enter a single character ch, and then the program will delete all the ch characters that appear in the string str to obtain a new one The string str2, and then print this string out.

Input format:
There are two lines of input, the first line is a string (no spaces inside), and the second line is a character.

Output format:
string after processing.
Sample input and output

Sample input:
123$45$678
$

Sample output:
12345678

c reference code:

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

char str[21];
char str2[21];
char ch;

int main()
{
    
    
    gets(str);
	scanf("%c",&ch);
	
	int i,j=0;
	for(i=0;i<strlen(str);i++)
	{
    
    
		if(str[i]==ch)
		 continue;
		str2[j++]=str[i];
    }
    
    for(i=0;i<strlen(str2);i++)
     printf("%c",str2[i]);
	return 0;
}

C++ reference code:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

char str[21];
char str2[21];
char ch;

int main()
{
    
    
    gets(str);
	
	scanf("%c",&ch);
	
	int j=0;
	for(int i=0;i<strlen(str);i++)
	{
    
    
		if(str[i]!=ch)
		 str2[j++]=str[i];
    }
    
    cout<<str2<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/115310707