How to remove the character at a given index from a string in C?

CString

C Problem Overview


How do I remove a character from a string?

If I have the string "abcdef" and I want to remove "b" how do I do that?

Removing the first character is easy with this code:

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

int main()
{
   char word[] = "abcdef";
   char word2[10];

   strcpy(word2,&word[1]);

   printf("%s\n", word2);
 
   return 0;
}

and

strncpy(word2,word,strlen(word)-1);

will give me the string without the last character, but I still didn't figure out how to remove a char in the middle of a string.

C Solutions


Solution 1 - C

memmove can handle overlapping areas, I would try something like that (not tested, maybe +-1 issue)

char word[] = "abcdef";  
int idxToDel = 2; 
memmove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);

Before: "abcdef"

After: "abdef"

Solution 2 - C

Try this :

void removeChar(char *str, char garbage) {

    char *src, *dst;
    for (src = dst = str; *src != '\0'; src++) {
	    *dst = *src;
	    if (*dst != garbage) dst++;
    }
    *dst = '\0';
}

Test program:

int main(void) {
    char* str = malloc(strlen("abcdef")+1);
    strcpy(str, "abcdef");
    removeChar(str, 'b');
    printf("%s", str);
    free(str);
    return 0;
}

Result:

>>acdef

Solution 3 - C

My way to remove all specified chars:

void RemoveChars(char *s, char c)
{
	int writer = 0, reader = 0;
	
	while (s[reader])
	{
		if (s[reader]!=c) 
		{	
			s[writer++] = s[reader];
		}
		
		reader++;		
	}
	
	s[writer]=0;
}

Solution 4 - C

Really surprised this hasn't been posted before.

strcpy(&str[idx_to_delete], &str[idx_to_delete + 1]);

Pretty efficient and simple. strcpy uses memmove on most implementations.

Solution 5 - C

char a[]="string";
int toBeRemoved=2;
memmove(&a[toBeRemoved],&a[toBeRemoved+1],strlen(a)-toBeRemoved);
puts(a);

Try this . memmove will overlap it. Tested.

Solution 6 - C

int chartoremove = 1;

strncpy(word2, word, chartoremove);
strncpy(((char*)word2)+chartoremove, ((char*)word)+chartoremove+1,
    strlen(word)-1-chartoremove);

Ugly as hell

Solution 7 - C

The following will extends the problem a bit by removing from the first string argument any character that occurs in the second string argument.

/*
 * delete one character from a string
 */
static void
_strdelchr( char *s, size_t i, size_t *a, size_t *b)
{
  size_t        j;

  if( *a == *b)
    *a = i - 1;
  else
    for( j = *b + 1; j < i; j++)
      s[++(*a)] = s[j];
  *b = i;
}

/*
 * delete all occurrences of characters in search from s
 * returns nr. of deleted characters
 */
size_t
strdelstr( char *s, const char *search)
{ 
  size_t        l               = strlen(s);
  size_t        n               = strlen(search);
  size_t        i;
  size_t        a               = 0;
  size_t        b               = 0;

  for( i = 0; i < l; i++)
    if( memchr( search, s[i], n))
      _strdelchr( s, i, &a, &b);
  _strdelchr( s, l, &a, &b);
  s[++a] = '\0';
  return l - a;
}

Solution 8 - C

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

int main(){
	char ch[15],ch1[15];
	int i;
	gets(ch);  // the original string
	for (i=0;i<strlen(ch);i++){  
		while (ch[i]==ch[i+1]){ 
			strncpy(ch1,ch,i+1); //ch1 contains all the characters up to and including x
			ch1[i]='\0'; //removing x from ch1
			strcpy(ch,&ch[i+1]);  //(shrinking ch) removing all the characters up to and including x from ch
			strcat(ch1,ch); //rejoining both parts
			strcpy(ch,ch1); //just wanna stay classy
		}
	}
	puts(ch);
}

Let's suppose that x is the "symbol" of the character you want to remove ,my idea was to divide the string into 2 parts:

1st part will countain all the characters from the index 0 till (and including) the target character x.

2nd part countains all the characters after x (not including x)

Now all you have to do is to rejoin both parts.

Solution 9 - C

Use strcat() to concatenate strings.

But strcat() doesn't allow overlapping so you'd need to create a new string to hold the output.

Solution 10 - C

Following should do it :

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

int main (int argc, char const* argv[])
{
    char word[] = "abcde";
    int i;
    int len = strlen(word);
    int rem = 1;

    /* remove rem'th char from word */
    for (i = rem; i < len - 1; i++) word[i] = word[i + 1];
    if (i < len) word[i] = '\0';

    printf("%s\n", word);
    return 0;
}

Solution 11 - C

I tried with strncpy() and snprintf().

int ridx = 1;  
strncpy(word2,word,ridx);	
snprintf(word2+ridx,10-ridx,"%s",&word[ridx+1]);

Solution 12 - C

Another solution, using memmove() along with index() and sizeof():

char buf[100] = "abcdef";
char remove = 'b';

char* c;
if ((c = index(buf, remove)) != NULL) {
	size_t len_left = sizeof(buf) - (c+1-buf);
	memmove(c, c+1, len_left);
}

buf[] now contains "acdef"

Solution 13 - C

Edit : Updated the code zstring_remove_chr() according to the latest version of the library.

From a BSD licensed string processing library for C, called zString

https://github.com/fnoyanisi/zString

Function to remove a character

int zstring_search_chr(char *token,char s){
	if (!token || s=='\0')
		return 0;

    for (;*token; token++)
        if (*token == s)
            return 1;

	return 0;
}

char *zstring_remove_chr(char *str,const char *bad) {
    char *src = str , *dst = str;
    
    /* validate input */
    if (!(str && bad))
        return NULL;
    
    while(*src)
        if(zstring_search_chr(bad,*src))
            src++;
        else
            *dst++ = *src++;  /* assign first, then incement */

    *dst='\0';
	return str;
}

Exmaple Usage

   char s[]="this is a trial string to test the function.";
   char *d=" .";
   printf("%s\n",zstring_remove_chr(s,d));

Example Output

  thisisatrialstringtotestthefunction

Solution 14 - C

This might be one of the fastest ones, if you pass the index:

void removeChar(char *str, unsigned int index) {
    char *src;
    for (src = str+index; *src != '\0'; *src = *(src+1),++src) ;
    *src = '\0';
}

Solution 15 - C

This code will delete all characters that you enter from string

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

#define	SIZE 1000

char *erase_c(char *p, int ch)
{
    char *ptr;

    while (ptr = strchr(p, ch))
		strcpy(ptr, ptr + 1);

    return p;
}

int main()
{
    char str[SIZE];
    int ch;

    printf("Enter a string\n");
    gets(str);
    printf("Enter the character to delete\n");
    ch = getchar();

    erase_c(str, ch);

    puts(str);

    return 0;
}

input

a man, a plan, a canal Panama

output

 A mn,  pln,  cnl, Pnm!

Solution 16 - C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
void dele_char(char s[],char ch)
{
    int i,j;

    for(i=0;s[i]!='\0';i++)
    {
        if(s[i]==ch)
        {
            for(j=i;s[j]!='\0';j++)
            s[j]=s[j+1];
            i--;
        }
    }
}

int main()
{
    char s[MAX],ch;
    printf("Enter the string\n");
    gets(s);
    printf("Enter The char to be deleted\n");
    scanf("%c",&ch);
    dele_char(s,ch);
    printf("After Deletion:= %s\n",s);
    return 0;
}

Solution 17 - C

This is what you may be looking for while counter is the index.

#include <stdio.h>

int main(){
        
    char str[20];
    int i,counter;
    gets(str);
    scanf("%d", &counter);

    for (i= counter+1; str[i]!='\0'; i++){
        str[i-1]=str[i];
    }
    str[i-1]=0;
    puts(str);


   
    return 0;
}

Solution 18 - C

I know that the question is very old, but I will leave my implementation here:

char    *ft_strdelchr(const char *str,char c)
{
        int   i;
        int   j;
        char  *s;
        char  *newstr;

        i = 0;
        j = 0;
        // cast to char* to be able to modify, bc the param is const
        // you guys can remove this and change the param too
        s = (char*)str;
        // malloc the new string with the necessary length. 
        // obs: strcountchr returns int number of c(haracters) inside s(tring)
        if (!(newstr = malloc(ft_strlen(s) - ft_strcountchr(s, c) + 1 * sizeof(char))))
                return (NULL);
        while (s[i])
        {
                if (s[i] != c)
                {
                        newstr[j] = s[i];
                        j++;
                }
                i++;
        }
        return (newstr);
}

just throw to a new string the characters that are not equal to the character you want to remove.

Solution 19 - C

This is a pretty basic way to do it:

void remove_character(char *string, int index) {
   for (index; *(string + index) != '\0'; index++) {
      *(string + index) = *(string + index + 1);
   }
}

Solution 20 - C

A convenient, simple and fast way to get rid of \0 is to copy the string without the last char (\0) with the help of strncpy instead of strcpy:

strncpy(newStrg,oldStrg,(strlen(oldStrg)-1));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFavolasView Question on Stackoverflow
Solution 1 - CstackerView Answer on Stackoverflow
Solution 2 - CFabio CabralView Answer on Stackoverflow
Solution 3 - CtBlabsView Answer on Stackoverflow
Solution 4 - CMD XFView Answer on Stackoverflow
Solution 5 - CSaravananKSView Answer on Stackoverflow
Solution 6 - CunexpectedvalueView Answer on Stackoverflow
Solution 7 - CDan the ManView Answer on Stackoverflow
Solution 8 - CWassim MohsniView Answer on Stackoverflow
Solution 9 - CDavid HeffernanView Answer on Stackoverflow
Solution 10 - CRumple StiltskinView Answer on Stackoverflow
Solution 11 - CPrince John WesleyView Answer on Stackoverflow
Solution 12 - Cpresto8View Answer on Stackoverflow
Solution 13 - CfnisiView Answer on Stackoverflow
Solution 14 - CÖzgürView Answer on Stackoverflow
Solution 15 - COrkun TokdemirView Answer on Stackoverflow
Solution 16 - CMushrif AliView Answer on Stackoverflow
Solution 17 - CiDreamView Answer on Stackoverflow
Solution 18 - CCaio V.View Answer on Stackoverflow
Solution 19 - CNikola SavicView Answer on Stackoverflow
Solution 20 - CMarcusView Answer on Stackoverflow