Exercise 5.3 - strcat(s,t) copies the string t to the end of s¶
Question¶
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 MAXLINE 1000
int mgetline(char line[], int lim);
void mystrcat(char *, char *);
int main(void) {
int len;
char s[MAXLINE], t[MAXLINE];
putchar('s');
putchar(':');
mgetline(s, MAXLINE);
putchar('t');
putchar(':');
mgetline(t, MAXLINE);
mystrcat(s, t);
printf("%s", s);
return 0;
}
int mgetline(char line[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
if (c == '\n') {
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
void mystrcat(char *s, char *t) {
while (*s != '\0')
s++;
s--; /* goes back to \0 char */
while ((*s = *t) != '\0') {
s++;
t++;
}
}
Explanation¶
This is a string concatenation program using pointers. The function mystrcat is defined to take two strings as character pointers mystrcat(char *s, char *t) and this function returns the concatenated string in s itself.
The way it does is, the position in s is advanced till we meet a 0 character and then we append the characters from the string t to s, starting from the 0 character till we hit the end of the string t which is a 0 again.
void mystrcat(char *s,char *t)
{
while(*s!='\0')
s++;
s--; /* goes back to \0 char */
while((*s=*t)!='\0')
{
s++;
t++;
}
}
The construct while((*s=*t)!=’0’) assigns the character in t to s and then checks if the character is 0.