C语言程序设计18

来源:百度知道 编辑:UC知道 时间:2024/06/02 06:25:05
下面程序的功能是:从键盘上输入一个字符串和一个字符;从字符串中删除所有指定字符;输出处理后的字符串,例如:输入字符串为:“turbo c and Borland c++”,输入字符为‘b’,则输出结果为:“turo c and Borland c++”。请输入并完善程序
#include<stdio.h>
void main()
{ char s[80],ch;
int i,j;
printf(“please input a string:”);
gets(s);
printf(“please input a charactor:”);
ch=getchar();
for(i=0;s[i]!=’\0’;i++)
if(s[i]==ch)
{ for( __________;s[j]!=’\0’;j++)
s[j-1]=s[j];
__________;
}
__________;
}

j=i+1;

s[--j]='\0';

printf("%s\n",s);

////////////////////////////////////////

为该项目写的这个程序并不完美,它不能处理这种情况:“既连续多个相同字符出现在字符串中,而要将这些相同字符全部去掉”。

对这个程序进行完善需要在for(i=0;s[i]!=’\0’;i++)
外面再加一个循环。

具体如下:

////////////////////////////////////
#include<stdio.h>
#include <conio.h>
void main()
{ char s[80],ch;
int i,j,k,sl;
printf("please input a string:");
gets(s);
printf("please input a charactor:");
ch=getchar();
//for (k=1;k<=strlen(s);k++)
for(i=0;s[i]!='\0';i++)
if(s[i]==ch)
{ for( j=i+1;s[j]!='\0';j++)
s[j-1]=s[j];
s[--j]='\0';
}
printf("%s\n",s);
getch();

}