From bc2a9bfb8108f37b43adf21598ebd88685beec3d Mon Sep 17 00:00:00 2001 From: Owen Dorweiler Date: Sat, 22 Mar 2025 20:47:26 -0400 Subject: [PATCH] Homework 06: str --- homework06/Makefile | 18 ++++--- homework06/str.c | 116 +++++++++++++++++++++++++++++++++++++++----- reading08/grep | Bin 0 -> 20288 bytes reading08/grep.c | 6 +++ 4 files changed, 121 insertions(+), 19 deletions(-) create mode 100755 reading08/grep create mode 100644 reading08/grep.c 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 0000000000000000000000000000000000000000..3620e3371693859db8c05b1ec8b361ecf3664890 GIT binary patch literal 20288 zcmeHPdu&_Rc|Uwfq$N?3U-2WU={4zWry?aqR;4(KNj<4TwiH`#Vzl-(MP5;iDN?w+ zw50$;j5uklWM1VBYm8wF`q!3X-PS*r!do#wnbXAC(4fJ()kQME-5S7KJIvi9OOwUg z?>qN=B6)df7{y*ecx>uB-|u_ebG~zU?>UF(+$TpSM%^x#;N%t$3*x4{Q3+XpksXt) z!umu&M8$3q5)DAS_(Y|IsFh>-o^s99uk<{iWY_1mfIfFrW|-b>!GbB5kf=||ED}|m zMXytVkeCXTkrLUFC>kV==&1_&wz|M|McIyuM|M-nZc5oPT~Yc>xqehP`b?|(O}nGu zh>sEx!}4#5k}jpGcgz&!Z>UP%@I?s*Z_ysf3Bi=@9)X>zy3)8kX;b1O>Uy=}va0IG zl*1FrW@dVOBH2_&Hj^tZb}aVwcJ%gy3;A%jtT*{5-Lb>R`6Bfk1b!!x}H+Q^na$lh_<3(b&&b`!9T4)_SLkOMwl0~e@wlB(G&G>!6F z!AuyYHlN7k#Ms36;E>iG?hfx4+W7P#Eu|ZJI#V!pWBSlgHlNd{6Ej&Img)I?PFZS} zs-q&sz>N?86N1~VxFZvwh~A^he!caS%uyepxN*8shI9N$cC!qpcHs108P0u+WdGXw ziTC5@f9j7v=lheUkcdCCYP#3H7eD`s{|cK*N_t)YG`{@i$@uc$#n1ov=G64~Ypbj1 zz46yxo|5FXUXZmdkHgKst&ske*>LNwr=|Hy{KCL8`M=z^BF zhgcLh9g8o&bK{A%wY67$Ya2qsb>hm(mDy6^T6aZd9DedFDK7uYf5w-KH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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; +}