[H-10]
Write function that shortens a string by
removing all instances of a particular character from a string. The null that
terminates the string is placed after its new last character. The prototype
of the function is:
void deleteCh( char *str, char ch );
Remove every ch
from str
.
For example , deleteCh( "applecart", 'a')
changes "applecart" into
"pplecrt". Here is a testing function:
#include <stdio.h> #include <stdlib.h> void deleteCh( char *str, char ch ) { } struct test { char str[50]; char ch; }; int main(int argc, char *argv[]) { struct test trials[] = { {"aXX", 'a'}, {"XXa", 'a'}, {"aXX", 'x'}, {"a", 'a'}, {"aa", 'a'}, {"aaa", 'a'}, {"bca", 'a'}, {"bcaa", 'a'}, {"aaabbbaaa", 'a'}, {"xaxaxa", 'a'}, {"XAAxaa", 'a'}, {"applecart", 'a'}, {"blue berry", 'e'} }; int j; for ( j=0; j<sizeof(trials)/sizeof(struct test); j++ ) { printf( "%s\t", trials[j].str ); deleteCh( trials[j].str, trials[j].ch ); printf( "%c\t%s\n", trials[j].ch, trials[j].str ); } return 0; }
Attempt to write this function without using a second string as a buffer or
by using malloc()
to get a buffer.