Title Description:
The input string s and the character C are required to remove all C characters from S and output the result.
Input:
The test data has multiple groups, each set of input string s and the character C.
Output:
For each set of inputs, the output removes the result after the C character.
Sample input:
Heallo
A
Sample output:
Hello
Problem Solving Code:
Solution 1: Use two arrays, the second array stores the strings that remove the specific characters
#include <stdio.h>intMain () {Chararr[ $]; Chararrnew[ $]; CharFocus; while(SCANF ("%s", arr)! =EOF) {GetChar (); scanf ("%c",&focus); intj =0; for(inti =0; Arr[i]! =' /'; i++){ if(Arr[i]! =Focus) {Arrnew[j]=Arr[i]; J++; }} Arrnew[j]=' /'; printf ("%s\n", arrnew); } return 0;}
Operation Result:
Solution 2: Use only one array, find a specific character offset once, overwrite the specific character
#include <stdio.h>#include<string.h>intMain () {Chararr[ $]; CharFocus; while(SCANF ("%s", arr)! =EOF) { intm =strlen (arr); GetChar (); scanf ("%c",&focus); for(inti =0; I < m; i++){ if(Arr[i] = =Focus) { for(intj = i +1; J < M; J + +) {arr[j-1] =Arr[j]; } I--; Arr[m-1] =' /'; M--; }} printf ("%s\n", arr); } return 0;}
Operation Result:
OJ Judging situation:
Solution 1:
Solution 2:
Algorithm Analysis: (the topic is also water problem)
Solution 1: Use two arrays, the second array stores the strings that remove the specific characters
Special attention is paid to the following:
while (scanf ("%s", arr)! = EOF) { getchar (); scanf ("%c", &focus);
Here's The GetChar (); Must be used, because%c, will put the enter key as a character, so that you can not enter a specific character.
Algorithmic complexity: O (n^2)
Solution 2: Use only one array, find a specific character offset once, overwrite the specific character
Note the following:
i--; Arr[m-1'+'; M--;
Offset once, the total string is less a character, this time is equivalent to the position of I +1, so to reduce one, the m-1 into a ' ", Explanation: The original string the last character is '", then reduce one, the second to the penultimate set to ' "".
String to go to specific characters-2009 the computer Research of Harbin Institute of Technology The real problem