diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-29 19:28:36 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-29 19:28:36 +0800 |
| commit | bbfec50dc1ee109b6980605eb0d19e6a4641968a (patch) | |
| tree | bb5669b680c19743ce50474b7cfa91aea5f02673 | |
| parent | 6cf3f3716a50c219b319978e1afd59423ea932cd (diff) | |
| download | k&r-exercises-bbfec50dc1ee109b6980605eb0d19e6a4641968a.tar.gz | |
5.3
| -rw-r--r-- | 5/3.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -0,0 +1,37 @@ +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> + +#define MAXLEN 1000 + +void mstrcat(char *, char *); + +int main(int argc, char *argv[]) { + char *s, *t; + size_t n; + + s = malloc(MAXLEN); + t = malloc(MAXLEN); + n = sizeof(s); + + printf("first str: "); + getline(&s, &n, stdin); + + printf("second str: "); + getline(&t, &n, stdin); + + mstrcat(s, t); + printf("strcat: %s\n", s); + + return 0; +} + +void mstrcat(char *s, char *t) { + int i; + + for (i = 0; i < MAXLEN - 1 && *s++; i++) + ; + for (s -= 2; i < MAXLEN - 1 && (*s++ = *t++) != '\n'; i++) + ; + *s = 0; +}
\ No newline at end of file |
