strcpy
This commit is contained in:
parent
5532922411
commit
87b3901e4e
1 changed files with 23 additions and 1 deletions
24
src/miniio.c
24
src/miniio.c
|
@ -9,12 +9,34 @@ int my_strlen(const char *s) {
|
||||||
return len;
|
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() {
|
int main() {
|
||||||
char word[] = "hallo\0"; // not NULL terminated
|
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 %d long\n", my_strlen(word));
|
||||||
printf("word is actually %lu long\n", 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue