Homework 06: str
This commit is contained in:
parent
f82bac6ce0
commit
bc2a9bfb81
4 changed files with 121 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
116
homework06/str.c
116
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: */
|
||||
|
|
|
|||
BIN
reading08/grep
Executable file
BIN
reading08/grep
Executable file
Binary file not shown.
6
reading08/grep.c
Normal file
6
reading08/grep.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Your pattern matching implementation
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue