0

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

  • You are correct, but you didn't implement it correctly. – timrau Nov 18 '13 at 15:36
  • The algorithm you describe is correct, but the code you posted does not do the same thing you describe – it only overwrites the deleted character with a copy of the following character. – Arkku Nov 18 '13 at 15:36
  • the algorithm will work only, when not more than one character is going to be removed. E.g. it will not remove 'a' correctly from 'abac' – ensc Nov 18 '13 at 15:44
  • 2
    I'm not convinced your use of 'in front of it' and 'one step back' ties in with ordinary English usage (and I realize English might not be your native language). Generally, if you have a string `"abc"` and you are deleting `b`, English speakers would regard `a` as being at the front, `c` as being at the back. What you say implies you might end up with `"aac"`. You actually need to copy the string over itself, except when you encounter the character to be deleted. You need two separate indexes (or pointers) to record 'copy from' or source position and 'copy to' or destination position. – Jonathan Leffler Nov 18 '13 at 15:49
  • 2
    Regarding _I want to make this program without pointers. Just plain simple code._ writing in C without using pointers is like riding a bike without tires. You will not get far. – ryyker Nov 18 '13 at 15:59
  • @ryyker; Oh please! He is a beginner. – haccks Nov 18 '13 at 16:00
  • Copy the string and avoid the character you don't want. ( done in 1 or 2 loops) – Gabson Nov 18 '13 at 16:01
  • @Gabson Jonathan Leffler: I tried to do like you explained (code is at the bottom of the OP) but I end up like weird symbols. – user3005287 Nov 18 '13 at 16:02
  • 1
    add `newString[i] = '\0';`, omg didn't noticed : you also have an `i++` too much – Gabson Nov 18 '13 at 16:08
  • @haccks - _Oh please_? Really? Pointers are _introduced_ the minute `strcpy()` or `strtok()` are taught, perhaps one week into a beginner's class. But this question is really of no consequence in this case, OP clarified at some point that he is _required_ to not use pointers :). Probably so it could be demonstrated to the students just how difficult life would be without pointers. (I am still surprised you would balk at such an inane comment though) – ryyker Nov 18 '13 at 18:02

2 Answers2

4
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';
ensc
  • 6,704
  • 14
  • 22
  • How can you perform `*out++ = *in` when `out` is a `char const *`? – timrau Nov 18 '13 at 15:38
  • I'm sorry but I can't use pointers. I will edit my main post. – user3005287 Nov 18 '13 at 15:44
  • I don't understand the way you are typing your code. Your way of typing code differs a lot from my way of typing it. I guess its because I am typing beginner's code which I want to keep doing, at least for this program... – user3005287 Nov 18 '13 at 15:54
  • 1
    @haccks - regarding _Providing a complete solution is not a good idea_. Who says? I see them all over this place on this site, and they are sometimes very instructional.. – ryyker Nov 18 '13 at 16:02
  • @haccks It really depends on the question, knowledge of who's asking the question, and the context of the question. Although, I do think ensc should explain the logic he is using so it's easier for new comers to C to read the code... – But I'm Not A Wrapper Class Nov 18 '13 at 16:30
  • 1
    @MohammadS.; I missed one word in that comment: *always*. – haccks Nov 18 '13 at 16:32
  • @haccks It's ok bro. I forgot one word too: <3 – But I'm Not A Wrapper Class Nov 18 '13 at 16:34
  • @user3005287 - regarding _I don't understand the way you are typing your code_. ***Under*** _without pointers_, what _ensc_ is doing is very clean and simple. Style is sometimes an issue, but not so much here. In this case, I would suggest you consider taking a close look, and perhaps adopting some of this style. (rather than desiring to keep typing _beginner's code_) – ryyker Nov 18 '13 at 17:40
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 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);
}
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264