diff --git a/homework06/Makefile b/homework06/Makefile index 8b7bc93..d3e8dda 100644 --- a/homework06/Makefile +++ b/homework06/Makefile @@ -13,21 +13,27 @@ all: $(TARGETS) # TODO: Add rules for libstr.a libstr.so #------------------------------------------------------------------------------- -str.o: +str.o: str.c + $(CC) $(CFLAGS) -c -o $@ $< -libstr.so: +libstr.so: str.o + $(LD) $(LDFLAGS) -shared -o $@ $< -libstr.a: +libstr.a: str.o + $(AR) $(ARFLAGS) $@ $< #------------------------------------------------------------------------------- # TODO: Add rules for trit.dynamic trit.static #------------------------------------------------------------------------------- -trit.o: +trit.o: trit.c + $(CC) $(CFLAGS) -c -o $@ $< -trit.dynamic: +trit.dynamic: trit.o + $(LD) $(LDFLAGS) -o $@ $< -lstr -trit.static: +trit.static: trit.o libstr.a + $(LD) -o $@ $< libstr.a #------------------------------------------------------------------------------- # DO NOT MODIFY BELOW diff --git a/homework06/str.c b/homework06/str.c index c05daa3..e4e5fb3 100644 --- a/homework06/str.c +++ b/homework06/str.c @@ -13,17 +13,27 @@ * @param s String to convert * @param w Pointer to buffer that holds result of conversion **/ -void str_lower(const char *s, char *w) { - // TODO -} + void str_lower(const char *s, char *w) { + while (*s) { + *w = tolower(*s); + s++; + w++; + } + *w = '\0'; + } /** * Convert string to uppercase. * @param s String to convert * @param w Pointer to buffer that holds result of conversion **/ -void str_upper(const char *s, char *w) { - // TODO +void str_upper(const char *s, char *w) { + while (*s) { + *w = toupper(*s); + s++; + w++; + } + *w = '\0'; } /** @@ -31,8 +41,22 @@ void str_upper(const char *s, char *w) { * @param s String to convert * @param w Pointer to buffer that holds result of conversion **/ -void str_title(const char *s, char *w) { - // TODO +void str_title(const char *s, char *w) { + if (*s) { + *w = toupper(*s); + w++; + s++; + while (*s) { + if (!isalpha(*(s - 1))) { + *w = toupper(*s); + } else { + *w = tolower(*s); + } + s++; + w++; + } + } + *w = '\0'; } /** @@ -41,8 +65,38 @@ void str_title(const char *s, char *w) { * @param chars Characters to strip (if NULL, then all whitespace) * @param w Pointer to buffer that holds result of strip **/ -void str_rstrip(const char *s, const char *chars, char *w) { - // TODO +void str_rstrip(const char *s, const char *chars, char *w) { + bool strip_chars[256] = {0}; + + // Create the lookup table + if (chars == NULL) { + // If chars is NULL, strip whitespace + strip_chars[' '] = true; + strip_chars['\t'] = true; + strip_chars['\n'] = true; + } else { + // Otherwise, strip specified characters + while (*chars) { + strip_chars[(unsigned char)*chars] = true; + chars++; + } + } + + // Copy s to w + const char *src = s; + char *dst = w; + while (*src) { + *dst++ = *src++; + } + *dst = '\0'; + + if (dst > w) { + dst--; + while (dst >= w && strip_chars[(unsigned char)*dst]) { + dst--; + } + *(dst + 1) = '\0'; + } } /** @@ -51,8 +105,24 @@ void str_rstrip(const char *s, const char *chars, char *w) { * @param chars Characters to delete * @param w Pointer to buffer that holds result of deletion **/ -void str_delete(const char *s, const char *chars, char *w) { - // TODO +void str_delete(const char *s, const char *chars, char *w) { + bool del_chars[256] = {0}; + + // Create lookup table + while (*chars) { + del_chars[(unsigned char)*chars] = true; + chars++; + } + + // Copy non-deleted chars to w + while (*s) { + if (!del_chars[(unsigned char)*s]) { + *w = *s; + w++; + } + s++; + } + *w = '\0'; } /** @@ -62,8 +132,28 @@ void str_delete(const char *s, const char *chars, char *w) { * @param to String with corresponding translation characters * @param w Pointer to buffer that holds result of translation **/ -void str_translate(const char *s, const char *from, const char *to, char *w) { - // TODO +void str_translate(const char *s, const char *from, const char *to, char *w) { + unsigned char translate_table[256]; + int i; + + for (i = 0; i < 256; i++) { + translate_table[i] = i; + } + + // Create the translation map + while (*from && *to) { + translate_table[(unsigned char)*from] = *to; + from++; + to++; + } + + // Translate + while (*s) { + *w = translate_table[(unsigned char)*s]; + s++; + w++; + } + *w = '\0'; } /* vim: set sts=4 sw=4 ts=8 expandtab ft=c: */ diff --git a/reading08/grep b/reading08/grep new file mode 100755 index 0000000..3620e33 Binary files /dev/null and b/reading08/grep differ diff --git a/reading08/grep.c b/reading08/grep.c new file mode 100644 index 0000000..e1f497c --- /dev/null +++ b/reading08/grep.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char *argv[]) { + // Your pattern matching implementation + return 0; +}