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 ;
}
}
CodePudding user response:
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
CodePudding user response:
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])
}