Exercise 8-6 Delete characters (20 points)
This question requires the implementation of a simple function to delete the specified characters in the string.
Function interface definition:
void delchar( char *str, char c );
Among them char *str
is the incoming string, which is the character c
to be deleted. The function delchar
of the function is to delete str
all c
characters appearing in the string.
Sample referee test procedure:
#include <stdio.h>
#define MAXN 20
void delchar( char *str, char c );
void ReadString( char s[] ); /* 由裁判实现,略去不表 */
int main()
{
char str[MAXN], c;
scanf("%c\n", &c);
ReadString(str);
delchar(str, c);
printf("%s\n", str);
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
a
happy new year
Sample output:
hppy new yer
//1. If you have any questions, please leave a message to point out, thanks! ! !
//2. If you have a better idea, you are also welcome to leave a message, thank you! ! !
void delchar( char *str, char c)
{int n,i,j;
n=strlen(str); strlen() is used to calculate the length of the specified string str, not including the end character "\0".
for(i=n-1;i>=0;i--)
{ if(str[i]==c) {j=i; while(j<n) { str[j]=str[j+1 ]; j++; } } } }