This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exercise 3-3: working with a-z and A-Z
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# include <stdio.h> | ||
# include <string.h> | ||
# include <assert.h> | ||
|
||
/* | ||
* Write a functiona expand(s1, s2) that expands shorthand | ||
* notations like a-z in the string s1 into the equivalent | ||
* complete list abc...xyz in s2. Allow for letters of either | ||
* case and digits and be prepared to handle cases like a-b-c | ||
* and a-z0-9 and -a-z. Arrange that a leading or trailing - | ||
* is taken literally. | ||
*/ | ||
|
||
# define MAXSTRINGLENGTH 1000 | ||
|
||
void expand(char s1[], char s2[]); | ||
|
||
void assert_equal(char expected[], char got[]) { | ||
int length_expected = strlen(expected), length_got = strlen(got); | ||
|
||
assert(length_expected == length_got); | ||
int i; | ||
|
||
for (i = 0; i < length_expected; i++) { | ||
assert(expected[i] == got[i]); | ||
} | ||
} | ||
|
||
void test_expand_a_z() { | ||
char s1[MAXSTRINGLENGTH] = "a-z", s2[MAXSTRINGLENGTH]; | ||
expand(s1, s2); | ||
assert(26 == strlen(s2)); | ||
assert_equal("abcdefghijklmnopqrstuvwxyz", s2); | ||
} | ||
|
||
void test_expand_A_Z() { | ||
char s1[MAXSTRINGLENGTH] = "A-Z", s2[MAXSTRINGLENGTH]; | ||
expand(s1, s2); | ||
assert(26 == strlen(s2)); | ||
assert_equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ", s2); | ||
} | ||
|
||
int main (int argc, char const* argv[]) { | ||
test_expand_a_z(); | ||
test_expand_A_Z(); | ||
return 0; | ||
} | ||
|
||
void expand(char s1[], char s2[]) { | ||
int i, c; | ||
|
||
for (i = 0, c = s1[0]; c <= s1[2]; i++, c++) { | ||
s2[i] = c; | ||
} | ||
|
||
s2[i] = '\0'; | ||
} |