简体   繁体   English

从字符串 C 中删除字符

[英]Removing char from string C

I'm learning C now I need to make a program that remove char that I'll input from string.我正在学习 C 现在我需要制作一个程序来删除我将从字符串中输入的字符。 I've seen an algorithm and I write this code我看过一个算法,我写了这段代码

#define MAX_LEN 200
int main()
{
    char str[MAX_LEN];
    char rem;
    int i = 0;
    
    printf("Enter the setence:");
    gets(str);
    printf("\nEnter the char to remove");
    rem = getchar();
    char* pDest = str;
    char* pS= str;
    printf("sent:\n%s", str);

    while (str[i]!='\0'){
        if (*pDest != rem) {
            *pDest = *pS;
            pDest++;
            pS++;
        }
        else if (*pDest == rem) {
            pS++;
        }
        i++;
    }
    *pDest = '\0';
    while (str[i] != '\0') {
        printf("number%d", i);
        putchar(str[i]);
        printf("\n");
        i++;
    }
}

But it returns nothing, like the value str gets, i think \0 and retuns nothing.但它什么也不返回,就像 str 得到的值一样,我认为 \0 并且什么也不返回。 May you help me to find the problem?你能帮我找出问题吗?

Use functions!!使用函数!!

If dest is NULL then this function will modify the string str otherwise, it will place the string with removed ch in dest .如果destNULL那么这个 function 将修改字符串str否则,它将把删除ch的字符串放在dest中。

It returns reference to the string with removed character.它返回对已删除字符的字符串的引用。

char *removeChar(char *dest, char *str, const char ch)
{
    char *head = dest ? dest : str, *tail = str;

    if(str)
    {
        while(*tail)
        {
            if(*tail == ch) tail++;
            else *head++ = *tail++;
        }
        *head = 0;
    }
    return dest ? dest : str;
}


int main(void)
{
    char str[] = "ssHeslsslsos sWossrlssd!ss";

    printf("Removal of 's' : `%s`\n", removeChar(NULL, str, 's'));
}

I just added new char array char dest[MAX_LEN] that store string with deleted symbols:我刚刚添加了新的 char 数组char dest[MAX_LEN]来存储带有已删除符号的字符串:

 #define MAX_LEN 200
    int main()
    {
        char str[MAX_LEN];
        char rem;
        int i = 0;
        
        printf("Enter the setence:");
        gets(str);
        printf("\nEnter the char to remove");
        rem = getchar();
        char dest[MAX_LEN] = "\0";
        char* pDest = dest;
        char* pS = str;
        printf("sent:\n%s", str);
    
        while (str[i]!='\0')
        {
            if (*pS != rem) 
            {
                *pDest = *pS;
                pDest++;
                pS++;
            }
            else if (*pS == rem) 
            {
                pS++;
            }
            i++;
        }
        i = 0;
        printf("\nres:\n %s \n", dest);
        while (dest[i] != '\0') {
            printf("number%d", i);
            putchar(dest[i]);
            printf("\n");
            i++;
        }
    }

It would be easier to use array style indexing to go through the string.通过字符串对 go 使用数组样式索引会更容易。 For example use str[i] = str[i + 1] instead of *pstr = *other_pstr .例如使用str[i] = str[i + 1]而不是*pstr = *other_pstr I leave this incomplete method, since this looks like homework.我留下了这个不完整的方法,因为这看起来像家庭作业。

int main()
{
    char str[] = "0123456789";
    char ch = '3';
    for (int i = 0, len = strlen(str); i < len; i++)
        if (str[i] == ch)
        {
            for (int k = i; k < len; k++)
            {
                //Todo: shift the characters to left 
                //Hint, it's one line
            }
            len--;
        }
    printf("%s\n", str);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM