Last active
November 17, 2016 16:29
-
-
Save gulan/42c58c60ad405add9205f899105abf27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<assert.h> | |
| #include<stdio.h> | |
| #include<string.h> | |
| /* The function squeeze removes all instances of specified characters | |
| from the input string array. The characters to be excluded are given in | |
| a second argument.*/ | |
| struct testrec { | |
| char inpu[30]; | |
| char *exclude; | |
| char *expect; | |
| }; | |
| struct testrec t0 = {"abcabcabc", "a", "bcbcbc"}; | |
| struct testrec t1 = {"abcabcabc", "b", "acacac"}; | |
| struct testrec t2 = {"abcabcabc", "c", "ababab"}; | |
| struct testrec t3 = {"abcabcabc", "ab", "ccc"}; | |
| struct testrec t4 = {"abcabcabc", "cb", "aaa"}; | |
| struct testrec t5 = {"abcabcabc", "cba", ""}; | |
| struct testrec t6 = {"abcabcabc", "", "abcabcabc"}; | |
| struct testrec t7 = {"abcabcabc", "xyz", "abcabcabc"}; | |
| struct testrec t8 = {"abcabcabc", "bbbbb", "acacac"}; | |
| struct testrec t9 = {"abcabcabc", "abcabcabc", ""}; | |
| struct testrec t10 = {"", "abc", ""}; | |
| struct testrec t11 = {"", "", ""}; | |
| int | |
| within(char ch, const char cset[]) { | |
| int i; | |
| i = 0; | |
| while (cset[i] != '\0' && cset[i] != ch) { | |
| i++; | |
| } | |
| assert (cset[i] == '\0' || cset[i] == ch); | |
| return (cset[i] != '\0'); /* IS within */ | |
| } | |
| void | |
| squeeze(char s[], const char exclude[]) { | |
| int i,j; | |
| i = j = 0; | |
| while (s[i] != '\0') { | |
| if (within(s[i], exclude)) | |
| i++; /* skip */ | |
| else | |
| s[j++] = s[i++]; | |
| } | |
| s[j] = '\0'; | |
| } | |
| void | |
| utest (struct testrec *tc) { | |
| char buf[256]; | |
| strcpy(buf, tc->inpu); | |
| squeeze(buf, tc->exclude); | |
| assert(strcmp(buf, tc->expect) == 0); | |
| printf("PASS\n"); | |
| } | |
| int | |
| main (int argc, char *argv[]) { | |
| utest(&t0); | |
| utest(&t1); | |
| utest(&t2); | |
| utest(&t3); | |
| utest(&t4); | |
| utest(&t5); | |
| utest(&t6); | |
| utest(&t7); | |
| utest(&t8); | |
| utest(&t9); | |
| utest(&t10); | |
| utest(&t11); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment