删除字符串中的字符。输入一个字符串s,再输入一个字符c,将字符串s中出现的所有字符c删除

【评分标准】首先打印提示Input a string:;然后直接在冒号后面输入字符串,字符串中可以包含空格;字符串以回车结束。
打印提示Input a char: 然后直接在冒号后面输入一个字符;回车。
【输出形式】
首先打印After deleted,the string is:紧跟后面输出被删除后的字符串剩余内容;换行。
【运行时的输入输出样例】(下划线部分表示输入)
Input a string:happy new year
Input a char:a
After deleted,the string is:hppy new yer

#include <stdio.h> 


int main () 
{
int i,n=0;
char ch,c[100];
printf("Input a string: ");
while ((ch=getchar()) != '\n'){
c[n++]=ch;
}
printf("Input a char: ");
scanf("%c",&ch); 
printf("After deleted,the string is: ");
for(i=0;i<n;i++)
if (c[i]!=ch)
printf("%c",c[i]);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-22
#include <stdio.h>
#include <string.h>
#define N 1024
void str_delete(char s[],char x)
{
int i,j,len;
i=0;
while(s[i]!='\0'){
if(s[i] == x){
for(j=i+1;s[j]!='\0';j++)
s[j-1]=s[j];
s[j-1]='\0';
i=0;
}
else
i++;
}
}
int main()
{
char str[N],ch;
printf("Input a string:");
gets(str);
printf("Input a char:");
ch=getchar();
str_delete(str,ch);
printf("After deleted,the string is:%s\n",str);
return 0;
}

//示例运行结果
F:\c_work>a.exe
Input a string:happy new year
Input a char:a
After deleted,the string is:hppy new yer

本回答被网友采纳
相似回答