This commit is contained in:
Christoph J. Scherr 2023-10-30 10:56:10 +01:00
parent 5532922411
commit 87b3901e4e

View file

@ -9,12 +9,34 @@ int my_strlen(const char *s) {
return len;
}
void my_strcpy(char *dest, const char *src) {
register int len = 0;
while (src[len]) {
len++;
}
len++; // we have a NULL byte
for (int i = 0; i < len; i++) {
dest[i] = src[i];
}
}
int main() {
char word[] = "hallo\0"; // not NULL terminated
char my_buf[6];
char std_buf[6];
printf("strlen\n");
printf("word is %d long\n", my_strlen(word));
printf("word is actually %lu long\n", strlen(word));
printf("success: %b", my_strlen(word) == strlen(word));
printf("success: %b\n", my_strlen(word) == strlen(word));
printf("\nstrcpy\n");
my_strcpy(my_buf, word);
strcpy(std_buf, word);
printf("word: \"%s\"\n", word);
printf("my_buf: \"%s\"\n", my_buf);
printf("std_buf: \"%s\"\n", std_buf);
printf("success: %b\n", 0 == strcmp(my_buf, std_buf));
return 0;
}