第二场考试最后一题 特别提醒:请注意数据范围
Problem G: 零起点学算法102——删除字符
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 14915 Solved: 2858
Description
从键盘输入任意一个字符串和一个字符,要求从该字符串中删除所有该字符。
Input
输入有多组测试数据。 每组两行,第一行是字符串(字符串至少还有一个字符,不多于100个),第二行是一个字符
Output
每组输出一行,删除了所有应删除字符后的字符串
Sample Input
ABCDE
E
ASD Dfg fhd
D
Sample Output
ABCD
AS fg fhd
HINT
char str[100],a;
while(gets(str)!=NULL)//多组测试数据
{
scanf("%c",&a);
getchar();//注意加这一行,它的作用是将上一行输入的回车符号’\n’符号读入
for(i=0;str[i]!=’\0’;i++) {
//写代码;找到a,用循环语句将数组左移1位置
}
//写代码;输出删除a后的数组
}
return 0;
}
//可能第二行的字符后面有空格或者其他的东西,建议用gets来读第二行。
也就是说第二行的一个字符要用一个数组来存储最好
真尼玛坑!
//用C++的可以把gets换成getline会更好
#include <stdio.h>
#include <string.h>
int main()
{
char str[110],ch[10];
//gets()函数把一行字符存入str中 ,最后的回车符会被舍弃
while (gets(str) != NULL)
{
gets(ch);
int i = 0;
while (str[i] != '\0')
{
if (str[i] != ch[0])
{
printf("%c",str[i]);
i++;
}
else i++;
}
printf("\n");
}
return 0;
}