-5

how can I remove a certain char from a string in c without using any library functions? the function i wrote seems to have a run time error and i can not figure out why.

void remove_char(char* s,int index)
{
    while(s[index+1] != 0)
       {
           s[index] = s[index + 1];
           index++;
       }
      s[index] = 0;
}

I was also wondering if there is a way to remove a char in a complexity of 1?

saar13531
  • 105
  • 2
  • 8
  • How do you use the function? What arguments do you pass it, what are their values? Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us? And you *have* tried [to debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Some programmer dude Feb 13 '18 at 20:03
  • Where are you removing a certain char in your code? – Kevin Feb 13 '18 at 20:03
  • 3
    A char cannot be removed from the middle of a string with O(1) complexity. – Christian Gibbons Feb 13 '18 at 20:04
  • 2
    The function is fine. The problem is how it's being called. Are you passing in a pointer to a string constant or an array? The former will cause a crash. – dbush Feb 13 '18 at 20:06
  • Oh, right, `s[index+1]` will get the null terminator on the last iteration. – Barmar Feb 13 '18 at 20:33
  • @dbush is probably right, see https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha – Barmar Feb 13 '18 at 20:34
  • 1
    [This function works as intended](https://ideone.com/4QDlH0). The problem is in the code you *don't* show. That's why you need to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Feb 13 '18 at 20:35
  • You get a fault simply because `0` is not the same as `\0`. To be clear the end of a string is set to `\0` by comparing to 0 you miss the exit condition and get an index out of bounds – Omer Dagan Feb 13 '18 at 20:35
  • 1
    @Dagan No, `'\0' == 0`. The escape `\0` inserts the octal representation of the integer value zero into the string or character. – Some programmer dude Feb 13 '18 at 20:36
  • http://idownvotedbecau.se/nomcve/ and http://idownvotedbecau.se/itsnotworking/ and http://idownvotedbecau.se/nodebugging/ – Some programmer dude Feb 13 '18 at 20:46
  • @Some programmer dude - you are correct.. Sorry verified when I got home my mistake – Omer Dagan Feb 14 '18 at 06:14

2 Answers2

0

Without seeing how you are calling your function, knowing the exact failure mode is difficult. Calling it with char string[] = "this is a string";, it worked fine. But as the comments suggest, some other forms of input strings may cause a problem.

I have used the following implementation with no problems. It removes all occurrences of a specified character:

int RemoveCharFromString(char *orig, char c, char *newStr)
{
    if(!orig) return -1;
    if(!newStr) return -1;
    int i=0, j=0;

    while (*(orig+i) != '\0')
    {
        if (*(orig+i) != c)
        {
            *(newStr+j) = *(orig+i);
            j++;
            i++;
        }
        else i++;
    }
    *(newStr+j) = '\0';
    return 0;
}

Or, as you requested, this one removes a character at a specified index:

int RemoveCharFromStringByIndex(char *orig, int index, char *newStr)
{
    if(!orig) return -1;
    if(!newStr) return -1;
    int i=0, j=0;

    while (*(orig+i) != '\0')
    {
        if (i != index)
        {
            *(newStr+j) = *(orig+i);
            j++;
            i++;
        }
        else i++;
    }
    *(newStr+j) = '\0';
    return 0;
}

Notice, in this implementation newStr is created by caller and must include space enough to contain the result.

You can certainly adapt these in any way you need to. An improvement would be to change the prototype to:

char * RemoveCharFromString(char *orig, char c);

Or

char * RemoveCharFromStringByIndex(char *orig, int index)

But that will be something you can do, if you wish it so.

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

I always like this for removing a specific char from a string:

int RemoveCharFromString(const char *src, char *dst, char c)
{
    const char *s;
    char       *d;

    if ((char *)0 != src && (char *)0 != dst) {

        for (d = dst, s = src; (*d = *s); s++) {

            if (c != *d)
                d++;
        }

        return 0;
    }
    return 1;
}

Only increment the destination pointer if it's not equal to the character being skipped. Sometimes you see this using the passed arguments src and dst directly, but way back when some compilers would produce more efficient code with separate pointers. The "const" simply allows you to pass a constant string as the source without producing a compiler error.

Whilom Chime
  • 366
  • 1
  • 9