diff options
author | Howard Mao <zhehao.mao@gmail.com> | 2015-12-04 16:52:25 -0800 |
---|---|---|
committer | Howard Mao <zhehao.mao@gmail.com> | 2015-12-04 16:52:25 -0800 |
commit | 82357114e9eecd3bec9374da9cd6ce4ea77d7e83 (patch) | |
tree | 6869792d022495423781eefb4b57c8f1fc4ca64b | |
parent | 529a6a3a0c42468bf815255697279e0e059a22db (diff) | |
download | pk-82357114e9eecd3bec9374da9cd6ce4ea77d7e83.zip pk-82357114e9eecd3bec9374da9cd6ce4ea77d7e83.tar.gz pk-82357114e9eecd3bec9374da9cd6ce4ea77d7e83.tar.bz2 |
implement strncmp
-rw-r--r-- | pk/string.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/pk/string.c b/pk/string.c index e896379..0e3911d 100644 --- a/pk/string.c +++ b/pk/string.c @@ -47,12 +47,19 @@ size_t strlen(const char *s) int strcmp(const char* s1, const char* s2) { + return strncmp(s1, s2, -1); +} + +int strncmp(const char *s1, const char *s2, size_t n) +{ unsigned char c1, c2; + int i = 0; do { c1 = *s1++; c2 = *s2++; - } while (c1 != 0 && c1 == c2); + i++; + } while (c1 != 0 && c1 == c2 && (n == -1 || i < n)); return c1 - c2; } |