Strncat Implementation in C

STRNCAT – append part of a string to another.
strncat appends at most N characters from the string “s2″ to the end of the string “s1″. If string “s2″ contains less than N characters, “strncat” will stop when it encounters the terminating ‘′.

Syntax :ptr = strncat( s1, s2, N )

char *s1: Points to a string terminated by the usual ‘′.
const char *s2: Points to a string whose characters will be appended to the end of “s1″.
size_t N: Maximum number of characters from string “s2″ that should be appended to “s1″.
char *ptr: points to the new string (“s1″).

char *(strncat)(char *s1, const char * s2, size_t n)
{
char *s = s1;
while (*s != ‘′)
s++;
while (n != 0 && (*s = *s2++) != ‘′) {
n–;
s++;
}
if (*s != ‘′)
*s = ‘′;
return s1;
}

Related posts:

  1. Strncpy Implementation in C
  2. Strstr Implementation in C

Comments are closed.