PTA Zhejiang University Edition "C Language Programming (3rd Edition)" Problem Set Exercise 8-6 Delete Characters (20 points)

                              

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 *stris the incoming string, which is the character cto be deleted. The function delcharof the function is to delete strall ccharacters 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++;             }               }     }









Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/106267385