1161: 零起点学算法68——删除字符(有问题)(已AC)

1161: 零起点学算法68——删除字符

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 1412  Accepted: 479
[Submit][Status][Web Board]

Description

从键盘输入任意一个字符串和一个字符,要求从该字符串中删除所有该字符。

Input

 

输入有多组测试数据。
每组两行,第一行是字符串(字符串至少还有一个字符,不多于100个),第二行是一个字符

Output

每组输出一行,删除了所有应删除字符后的字符串

Sample Input

 
ABCDE
E
ASD Dfg fhd
D

Sample Output

ABCD
AS fg fhd

Source

 
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char ch1[100],ch2[100];
 5     while(gets(ch1)!=NULL){
 6         char c;
 7         scanf("%c",&c);    
 8         getchar();
 9         int j=0;
10         for(int i=0;ch1[i]!='';i++){
11             if(ch1[i]!=c){
12                 ch2[j++]=ch1[i];
13             }
14         }
15         
16         puts(ch2);
17     }
18     return 0;
19 } 

 //AC代码!

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char ch1[100];
 5     while(gets(ch1)!=NULL){
 6         char c;
 7         scanf("%c",&c);    
 8         getchar();
 9         for(int i=0;ch1[i]!='';i++){
10             if(ch1[i]==c){
11                 for(int j=i;ch1[j]!='';j++){
12                     ch1[j]=ch1[j+1];
13                 }
14                 i--;
15             }
16         }
17         
18         puts(ch1);
19     }
20     return 0;
21 }

思路就是 找到了就删除 用后面的覆盖前面的。 我也不知道第一种为什么出错。

原文地址:https://www.cnblogs.com/dddddd/p/6685513.html