Page 107
Write a pointer version of the function strcat
that we showed in Chapter 2:
strcat(s,t)
copies the string t
to the end of s
.
#include <stdio.h>
#define MAXLEN 1000
int str_cat(char *s, char *t);
int main()
{
char str1[MAXLEN] = "Hello";
char *str2 = " world!";
str_cat(str1, str2);
printf("%s\n", str1);
}
int str_cat(char *s, char *t)
{
int i;
for (i = 0; *s != '\0'; s++, i++)
;
for (; (*s = *t) != '\0'; s++, t++, i++)
;
return i;
}