Topic:

Programmatically removes the specified character from the given string. To delete a specified character, the original character string cannot leave empty space. Both the character string and the specified character are input by the keyboard

The basic idea

Compares the character string with the character to be deleted. If the character is the same, the character in the string is replaced with the next character in the original string, and the next character is advanced in order to delete the character. Note: After the character is moved one bit forward, you need to determine whether the character moved to the current position needs to be deleted.

Algorithm description

  1. Enter a string and a specified character from the keyboard
  2. Aligning strings and characters in a loop until the end of the string
  3. If no character is specified in the string, it is not changed. If a specified character exists, the subscripts of the characters after the character are successively advanced one bit
  4. The loop control variable is reduced by one to determine whether the character at this position is the specified character again, and repeat steps 2 and 3

Code implementation

# include<stdio.h>
# include<string.h>
int main() { int i, j, k; char a[1000]; char b[1000]; gets(a); // Enter the string gets(b); // Enter the specified character (can be multiple characters)for(i=0; a[i]! ='\ 0'; I++)// traversing array a {for(j=0; b[j]! ='\ 0'; J++)// iterate over b array {if(a[I]==b[j]) (a[I]==b[j])for(k=i; a[k]! ='\ 0'; K ++)// start loop assignment a[k]=a[k+ I]; i--; // The next loop will reach the newly assigned character position, continue to compare}}}printf("%s",a);
}
Copy the code

Algorithm analysis

The key points of the algorithm are completion and re-judgment of the position after completion