diff --git a/8-pointers-c-strings/CMakeLists.txt b/8-pointers-c-strings/CMakeLists.txt index 4ea2c1f..2d97647 100644 --- a/8-pointers-c-strings/CMakeLists.txt +++ b/8-pointers-c-strings/CMakeLists.txt @@ -1,5 +1,8 @@ add_executable(str-literals str-literals.c) + add_executable(strlen strlen.c) add_executable(strcpy strcpy.c) add_executable(strcmp strcmp.c) -add_executable(strcat strcat.c) \ No newline at end of file +add_executable(strcat strcat.c) + +add_executable(strcmp-benchmark strcmp-benchmark.c) \ No newline at end of file diff --git a/8-pointers-c-strings/str-literals.c b/8-pointers-c-strings/str-literals.c index 531e0f6..a1fd538 100644 --- a/8-pointers-c-strings/str-literals.c +++ b/8-pointers-c-strings/str-literals.c @@ -1,9 +1,5 @@ -// -// Created by hfwei on 2023/11/30. -// Visualization: -// https://pythontutor.com/render.html#code=%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%0Aint%20main%28void%29%20%7B%0A%20%20char%20msg%5B%5D%20%3D%20%22Hello%20World!%22%3B%0A%20%20msg%5B0%5D%20%3D%20'N'%3B%0A%20%20printf%28%22%25s%5Cn%22,%20msg%29%3B%0A%0A%20%20char%20*ptr_msg%20%3D%20%22Hello%20World!%22%3B%0A%20%20ptr_msg%5B0%5D%20%3D%20'N'%3B%0A%20%20printf%28%22%25s%5Cn%22,%20msg%29%3B%0A%0A%20%20return%200%3B%0A%7D&cumulative=true&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// Created by hfwei on 2024/11/20. // See String literals: https://en.cppreference.com/w/c/language/string_literal -// #include #include diff --git a/8-pointers-c-strings/strcmp.c b/8-pointers-c-strings/strcmp.c index 44b5079..e8e23f7 100644 --- a/8-pointers-c-strings/strcmp.c +++ b/8-pointers-c-strings/strcmp.c @@ -1,7 +1,4 @@ -// -// file: strcmp.c -// Created by hfwei on 2022/11/29. -// +// Created by hfwei on 2024/11/20. #include diff --git a/8-pointers-c-strings/strcpy.c b/8-pointers-c-strings/strcpy.c index 18082f4..9d476f1 100644 --- a/8-pointers-c-strings/strcpy.c +++ b/8-pointers-c-strings/strcpy.c @@ -1,6 +1,4 @@ -// file: strcpy.c -// 7 versions of strcpy -// Created by hfwei on 2022/11/29. +// Created by hfwei on 2024/11/20. // // C Operator Precedence: // https://en.cppreference.com/w/c/language/operator_precedence#:~:text=C%20Operator%20Precedence%20%20%20%20Precedence%20,union%20member%20access%20%2028%20more%20rows%20 diff --git a/8-pointers-c-strings/strlen.c b/8-pointers-c-strings/strlen.c index 90bea3a..2527569 100644 --- a/8-pointers-c-strings/strlen.c +++ b/8-pointers-c-strings/strlen.c @@ -1,4 +1,3 @@ -// file: strlen.c // Created by hfwei on 2024/11/20. // See https://en.cppreference.com/w/c/string/byte/strlen diff --git a/8-pointers-c-strings/strncmp.c b/8-pointers-c-strings/strncmp.c deleted file mode 100644 index 1d1d521..0000000 --- a/8-pointers-c-strings/strncmp.c +++ /dev/null @@ -1 +0,0 @@ -// Created by hfwei on 2024/11/20. \ No newline at end of file diff --git a/pdf/8-pointers-c-strings.pdf b/pdf/8-pointers-c-strings.pdf new file mode 100644 index 0000000..cee0ba9 Binary files /dev/null and b/pdf/8-pointers-c-strings.pdf differ diff --git a/template/8-pointers-c-strings/8-pointers-c-strings-template.pdf b/template/8-pointers-c-strings/8-pointers-c-strings-template.pdf new file mode 100644 index 0000000..508876f Binary files /dev/null and b/template/8-pointers-c-strings/8-pointers-c-strings-template.pdf differ diff --git a/template/8-pointers-c-strings/CMakeLists.txt b/template/8-pointers-c-strings/CMakeLists.txt new file mode 100644 index 0000000..5ac71c7 --- /dev/null +++ b/template/8-pointers-c-strings/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(str-literals str-literals.c) + +add_executable(strlen strlen.c) +add_executable(strcpy strcpy.c) +add_executable(strcmp strcmp.c) +add_executable(strcat strcat.c) \ No newline at end of file diff --git a/template/8-pointers-c-strings/README.md b/template/8-pointers-c-strings/README.md new file mode 100644 index 0000000..16a43d4 --- /dev/null +++ b/template/8-pointers-c-strings/README.md @@ -0,0 +1,13 @@ +# `9-more-pointers` + +## `strlen.c` + +- C string literal +- `while (str[len++] != '\0')` vs. + `while (++str[len] != '\0')` vs. + `while (++str[len])` +- `\0` vs. `0` + +## `strcpy.c` + +## `strcmp.c` \ No newline at end of file diff --git a/template/8-pointers-c-strings/str-literals.c b/template/8-pointers-c-strings/str-literals.c new file mode 100644 index 0000000..ad7f792 --- /dev/null +++ b/template/8-pointers-c-strings/str-literals.c @@ -0,0 +1,20 @@ +// Created by hfwei on 2024/11/20. +// Visualization: +// https://pythontutor.com/render.html#code=%23include%20%3Cstdio.h%3E%0A%0Aint%20main%28void%29%20%7B%0A%20%20char%20msg%5B%5D%20%3D%20%22Hello%20World!%22%3B%0A%20%20msg%5B0%5D%20%3D%20'N'%3B%0A%20%20printf%28%22%25s%5Cn%22,%20msg%29%3B%0A%0A%20%20char%20*ptr_msg%20%3D%20%22Goodbye%20World!%22%3B%0A%20%20ptr_msg%5B0%5D%20%3D%20'N'%3B%0A%20%20printf%28%22%25s%5Cn%22,%20msg%29%3B%0A%0A%20%20return%200%3B%0A%7D&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// See String literals: https://en.cppreference.com/w/c/language/string_literal + +#include +#include +#include + +int main(void) { + char msg[] = "Hello World!"; + msg[0] = 'N'; + printf("%s\n", msg); + + char *ptr_msg = "Goodbye World!"; + ptr_msg[0] = 'N'; + printf("%s\n", msg); + + return 0; +} \ No newline at end of file diff --git a/template/8-pointers-c-strings/strcat.c b/template/8-pointers-c-strings/strcat.c new file mode 100644 index 0000000..63967e9 --- /dev/null +++ b/template/8-pointers-c-strings/strcat.c @@ -0,0 +1,27 @@ +// Created by hfwei on 2024/11/20. + +#include +#include + +char *StrCat(char *s1, const char *s2); +char *StrCatGLibC(char *dest, const char *src); + +char *StrNCat(char *s1, const char *s2, size_t n); +size_t StrNLen(const char *s, size_t max); +char *StrNCatGLic(char *s1, const char *s2, size_t n); + +int main(void) { + char str[50] = "Hello "; + char str2[50] = "World!"; + +// strcat(str, str2); +// strcat(str, " ..."); +// strcat(str, " Goodbye World!"); + +// strcat(str, str2); +// strncat(str, " Goodbye World!", 3); + + puts(str); + + return 0; +} \ No newline at end of file diff --git a/template/8-pointers-c-strings/strcmp.c b/template/8-pointers-c-strings/strcmp.c new file mode 100644 index 0000000..95743d9 --- /dev/null +++ b/template/8-pointers-c-strings/strcmp.c @@ -0,0 +1,25 @@ +// Created by hfwei on 2024/11/20. + +#include + +int StrCmp(const char *s1, const char *s2); +int StrCmpStd(const char *s1, const char *s2); +int StrCmpGLibC(const char *p1, const char *p2); + +int StrNCmpStd(const char *s1, const char *s2, int n); + +int main() { + const char *str1 = "hi, C"; + const char *str2 = "hi, c"; + + printf("StrCmp(\"%s\", \"%s\") = %d\n", str1, str2, StrCmp(str1, str2)); + + // printf("StrCmpStd(\"%s\", \"%s\") = %d\n", + // str1, str2, StrCmpStd(str1, str2)); + // + // int n = 2; + // printf("StrNCmp(\"%s\", \"%s\", %d) = %d\n", + // str1, str2, n, StrNCmp(str1, str2, n)); + + return 0; +} \ No newline at end of file diff --git a/template/8-pointers-c-strings/strcpy.c b/template/8-pointers-c-strings/strcpy.c new file mode 100644 index 0000000..57fca47 --- /dev/null +++ b/template/8-pointers-c-strings/strcpy.c @@ -0,0 +1,27 @@ +// Created by hfwei on 2024/11/20. +// +// C Operator Precedence: +// https://en.cppreference.com/w/c/language/operator_precedence#:~:text=C%20Operator%20Precedence%20%20%20%20Precedence%20,union%20member%20access%20%2028%20more%20rows%20 + +#include +#include +#include + +void StrCpy1(char *dest, const char *src); +void StrCpy2(char *dest, const char *src); +void StrCpy3(char *dest, const char *src); +void StrCpy4(char *dest, const char *src); +void StrCpy5(char *dest, const char *src); +void StrCpy6(char *dest, const char *src); + +int main() { + const char *src = "Hello World"; + char *dest = malloc(strlen(src) + 1); + + StrCpy5(dest, src); + printf("dest = %s\n", dest); + +// strlen(StrCpyStd(dest, src)); + + return 0; +} \ No newline at end of file diff --git a/template/8-pointers-c-strings/strlen.c b/template/8-pointers-c-strings/strlen.c new file mode 100644 index 0000000..2680f64 --- /dev/null +++ b/template/8-pointers-c-strings/strlen.c @@ -0,0 +1,18 @@ +// file: strlen.c +// Created by hfwei on 2024/11/20. +// See https://en.cppreference.com/w/c/string/byte/strlen + +#include + +int StrLen1(const char *s); +int StrLen2(const char *s); +int StrLen3(const char *s); + +int main() { + char msg[] = "Hello World!"; + +// printf("StrLen(%s) = %d\n", msg, StrLen1(msg)); +// printf("StrLenStd(%s) = %zu\n", msg, StrLenStd(msg)); + + return 0; +} \ No newline at end of file