练习03-11-01
用一个字符串,内有若干个字符,现在输入一个字符,要求程序将字符串中该字符删去。
输出样例:
//样例一:
This is a C program.
Input the character you will delete.
.
This is a C program
//样例二:
This a C program
Input the character you will delete.
a
This C progrm
代码如下:
//有一个字符串,内有若干个字符,现输入一个字符,要求将字符串中该字符删去
#include<stdio.h>
#include<string.h>
int main()
{
void enter_string(char str[]);
void delete_string(char str[],char ch);
void printf_string(char str[]);
char str[80],ch;
enter_string(str);
printf("Input the character you will delete.\n");
scanf("%c",&ch);
delete_string(str,ch);
printf_string(str);
return 0;
}
void enter_string(char str[])
{
gets(str);
}
void delete_string(char str[],char ch)
{
int i=0;
void move_string(char str[],int n);
int count=-1;
for(i=0;str[i]!='\0';++i){
if(str[i]==ch){
//++count;
move_string(str,i);
}
}
}
void move_string(char str[],int n)
{
int i;
for(i=n+1;i<=strlen(str);++i){
str[i-1]=str[i];
}
//str[i]='\0';
}
void printf_string(char str[])
{
printf("%s",str);
}
简洁代码:
#include<stdio.h>
#define N 40
int main()
{
char ch[N],c;
printf("Input the string:\n");
gets(ch);
printf("Input the character you will delete:\n");
c=getchar();
int i,j;
for(i=0,j=0;ch[i]!='\0';++i){
if(ch[i]!=c){
ch[j++]=ch[i];
}
}
ch[j]='\0';
puts(ch);
return 0;
}