Wolo
Wolo

Reputation: 5

How do I remove multiple characters from a string in C?

currently I'm able to remove a single character from a string but I can't figure out how I can setup "symbol" so that it can remove multiple characters... any suggestions ?

#include <string.h>

void remove_character(char *string, char *remove);

int main(void){
    char s[] = "Thi!s is s!ome tex!t that we'!ll check for plagiarism";
    char symbol[] = "!";

    printf("%s\n", s);
    remove_character(s, symbol);
    printf("%s\n", s);

    return(0);
}
 
void remove_character(char *string, char *remove){
    int position = 0;

    while (string[position] != '\0')
    {
        if (string[position] == *remove){
            
            int newposition = position;

            while (string[newposition] != '\0')
            {
                string[newposition] = string[newposition+1];
                newposition++;
            }
            
        } else position++;
    }  
    

}
 

Upvotes: 0

Views: 384

Answers (3)

Jan Bergstr&#246;m
Jan Bergstr&#246;m

Reputation: 766

You have to copy the entire rest of the string else you are only replacing one char with another, use memmove.

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

void main(void)
{
    char String[] = { "Hello" };
    char Remove = 'l';
    char* pSearch = String;

    while (*pSearch) // is not 0
    {
        if (*pSearch == Remove)
            memmove(pSearch, pSearch + 1, strlen(pSearch) + 1);
        else 
            pSearch++;
    }
    printf("%s\n", String);
}

Upvotes: 0

Fe2O3
Fe2O3

Reputation: 8344

You don't have to "compact" the string each and every time the function encounters a character to be removed.

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

// Define the function ahead of its use. It is its own prototype.
void remove_character( char *str, char *remove ) {
    // copy characters, but only increment destination for "non-remove" characters.
    for( size_t src = 0, dst = 0; ( str[dst] = str[src] ) != '\0'; src++ )
        dst += (strchr( remove, str[dst] ) == NULL);
}

int main( void ) {
    char s[] = "Thi!s is s!ome tex!t that we'!ll check for plagiarism";
    char symbols[] = "!s"; // Stripping out '!' and 's'

    puts( s ); // simpler
    remove_character( s, symbols );
    puts( s ); // simpler

    return 0; // return is NOT a function call. No '()'.
}
Thi!s is s!ome tex!t that we'!ll check for plagiarism
Thi i ome text that we'll check for plagiarim

Upvotes: 1

Filipe
Filipe

Reputation: 158

If you can make it remove 1 just loop the fuction for an arrays of removable caracter

void remove_character(char *string, char remove){
    int position = 0;

    while (string[position] != '\0')
    {
        if (string[position] == remove){
            
            int newposition = position;

            while (string[newposition] != '\0')
            {
                string[newposition] = string[newposition+1];
                newposition++;
            }
            
        } else position++;
    }  
    

}

void remove_character(char *string, char *remove){

     for(each char in remove){
remove_character(string, remove[index])
}



Upvotes: 0

Related Questions