user3005287
user3005287

Reputation: 21

Remove a character from a string

I am trying to create a function that removes a specified 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. 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.

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?

        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...

Upvotes: 0

Views: 276

Answers (2)

haccks
haccks

Reputation: 105992

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 1and this l is not deleted by your function. 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);
}

Upvotes: 0

ensc
ensc

Reputation: 6984

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';

Upvotes: 4

Related Questions