简体   繁体   English

从字符串中删除字符

[英]Remove a character from a string

I am trying to create a function that removes a specified character from a string . 我正在尝试创建一个function ,该function character from a stringremoves指定的character from a string

I am only halfway done with the code because I got stuck when I am trying to replace the character to delete with nothing. 我只完成了一半的代码,因为在尝试替换要删除的字符时卡住了。 I just realized I can't put "nothing" in an element of an array so my plan just got ruined. 我只是意识到我不能在数组的元素中放“空”,所以我的计划被毁了。

I figure that I have to loop through the whole string, and when I find the character I want to remove I have to remove it by moving all of the elements that are in front of the "bad" character one step back. 我认为我必须遍历整个字符串,当我找到要删除的字符时,我必须通过将“坏”字符前面的所有元素都后退一步来删除它。 Is that correct? 那是对的吗?

#include <stdio.h>
#include <string.h>

void del(char string[], char charToDel)
{

    int index = 0;

    while(string[index] != '\0')
    {
        if(string[index] == charToDel){
            string[index] = string[index+1];
        }

        index++;
    }

    printf("%s", string);
}

int main(void)
{

    char string[] = "Hello world";

    del(string, 'l');

    return 0;
}

I want to make this program without pointers . 我想使这个程序without pointers Just plain simple code. 只是简单的简单代码。

I added another while loop that moves every character in the loop to the left but it doesn't seem to work since the output is just plain blank. 我添加了另一个while循环,该循环将循环中的每个字符向左移动,但由于输出只是纯空白,因此似乎不起作用。

int index = 0;


    while(string[index] != '\0')
       {
           if(string[index] == charToDel)
           {
                while(string[index] != '\0')
                {
                    string[index] = string[index+1];
                }

           }

           index++;
       }

    printf("%s", string);
}

Johathan Leffler's Method? Johathan Leffler的方法?

        char newString[100];

    int index = 0;
    int i = 0;

    while(string[index] != '\0')
       {
           if(string[index] != charToDel)
           {
                newString[i] = string[index];

                index++;
                i++;

           }
           i++;
           index++;
       }

    printf("%s", newString);
}

This gives me a lot of weird characters... 这给了我很多奇怪的角色...

char const *in = string;
char *out = string;

while (*in) {
    if (*in != charToDel)
        *out++ = *in;
    ++in;
}

*out = '\0';

or without pointers 或没有指针

size_t in = 0;
size_t out = 0;

while (string[in]) {
    if (string[in] != charToDel)
         string[out++] = string[in];
    ++in;
}

string[out] = '\0';

The problem is that, when you are assigning string[index+1] to string[index] , the next l from the string took place of previous one and index incremented to its next value by 1 and this l is not deleted by your function. 问题是,当您将string[index+1]分配给string[index+1]string[index]的下一个l代替了上一个,而index递增其下一个值1而该l未被函数删除。 You should have to fixed that. 您应该修复该问题。
As suggested by Jonathan Leffler and Gabson ; 正如乔纳森勒夫 加布森所建议的; you can do it by coping the string to itself as; 您可以通过将字符串本身匹配为;

void del(char string[], char charToDel)
{

    int index = 0, i = 0;

    while(string[index] != '\0')
    {
        if(string[index] != charToDel){
            string[i++] = string[index];
        }
        index++;
    }
    string[i] = '\0';

    printf("%s", string);
}

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

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