Professional Documents
Culture Documents
New Text Document
New Text Document
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void rmchr(char s1[], char a);
void testIt(char *, char);
char s1[] = "abracadabra";
char s2[] = "hello, world.";
char s3[] = "zzzzz";
char s4[] = "w";
const char c1 = 'a';
const char c2 = 'l';
const char c3 = 'z';
const char c4 = 'w';
int main(int argc, char *argv[]) {
testIt(s1,c1);
testIt(s2,c2);
testIt(s2,c2);
testIt(s3,c3);
testIt(s4,c4);
return 0;
}
/*
* rmchr( ): remove 'a' from the string 's'
*/
void rmchr(char s1[], char a) {
char *ptr = s1; /* Pointer to traverse array */
char *sptr; /* Pointer for skipped chars */
if (strchr(s1,a) != NULL) {
while (*ptr != '\0') {
if (*ptr == a) {
for (sptr = ptr; *sptr == a; ++sptr);
memcpy(ptr,sptr,&s1[strlen(s1)+1] - sptr);
}
if (*ptr != '\0') ++ptr;
} /* end while not end of string*/
} /* end if a is in s1 */
} /* end rmchr( ) */
void testIt(char *s, char c) {
printf("The string is \"%s\" with %c.\n", s, c);
rmchr(s, c);
printf("The string is \"%s\" without %c.\n", s, c);
puts("");
}
#if 0
Program output:
The string is "abracadabra" with a.
The string is "brcdbr" without a.
The string is "hello, world." with l.
The string is "heo, word." without l.
The string is "heo, word." with l.
The string is "heo, word." without l.
The string is "zzzzz" with z.
The string is "" without z.
The string is "w" with w.
The string is "" without w.
#endif