Skip to content

Commit

Permalink
template/: +8-pointer-c-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Nov 20, 2024
1 parent 5090acb commit 2c0d456
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 15 deletions.
5 changes: 4 additions & 1 deletion 8-pointers-c-strings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
add_executable(strcat strcat.c)

add_executable(strcmp-benchmark strcmp-benchmark.c)
6 changes: 1 addition & 5 deletions 8-pointers-c-strings/str-literals.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
Expand Down
5 changes: 1 addition & 4 deletions 8-pointers-c-strings/strcmp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//
// file: strcmp.c
// Created by hfwei on 2022/11/29.
//
// Created by hfwei on 2024/11/20.

#include <stdio.h>

Expand Down
4 changes: 1 addition & 3 deletions 8-pointers-c-strings/strcpy.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion 8-pointers-c-strings/strlen.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// file: strlen.c
// Created by hfwei on 2024/11/20.
// See https://en.cppreference.com/w/c/string/byte/strlen

Expand Down
1 change: 0 additions & 1 deletion 8-pointers-c-strings/strncmp.c

This file was deleted.

Binary file added pdf/8-pointers-c-strings.pdf
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions template/8-pointers-c-strings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions template/8-pointers-c-strings/README.md
Original file line number Diff line number Diff line change
@@ -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`
20 changes: 20 additions & 0 deletions template/8-pointers-c-strings/str-literals.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
27 changes: 27 additions & 0 deletions template/8-pointers-c-strings/strcat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Created by hfwei on 2024/11/20.

#include <stdio.h>
#include <string.h>

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;
}
25 changes: 25 additions & 0 deletions template/8-pointers-c-strings/strcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Created by hfwei on 2024/11/20.

#include <stdio.h>

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;
}
27 changes: 27 additions & 0 deletions template/8-pointers-c-strings/strcpy.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
18 changes: 18 additions & 0 deletions template/8-pointers-c-strings/strlen.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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;
}

0 comments on commit 2c0d456

Please sign in to comment.