Strip a set of characters from a string: Difference between revisions

Content added Content deleted
(+Common Lisp)
(→‎C: Added C implementation)
Line 4: Line 4:
<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei")
<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei")
Sh ws soul strppr. Sh took my hrt!</lang>
Sh ws soul strppr. Sh took my hrt!</lang>

=={{header|C}}==
<lang c>#include <string.h>
#include <malloc.h>
#include <stdio.h>

/* checks if character exists in list */
int contains( char character, char * list ){
while( *list ){
if( character == *list )
return 1;
++ list;
}
return 0;
}


/* removes all chars from string */
char * strip_chars( char * string, char * chars ){
char * newstr = malloc( strlen( string ) );
int counter = 0;

while( *string ){
if( contains( *string, chars ) != 1 ){
newstr[ counter ] = *string;
++ counter;
}
++ string;
}

return newstr;
}


int main( int argc, char ** argv ){
char * new = strip_chars( "She was a soul stripper. She took my heart!", "aei" );
printf( "%s\n", new );

free( new );

return 0;
}</lang>

Result:

<pre>Sh ws soul strppr. Sh took my hrt!</pre>



=={{header|C++}}==
=={{header|C++}}==