Exercise 5.5 - simple versions of strncpy, strncat, and strncmp

Question

Write versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings.

/* Write versions of the library functions strncpy, strncat, and strncmp, which
operate on at most the first n characters of their argument strings.*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXSIZE 1000
void mystrncpy(char *,char *,int);
void mystrncat(char *, char *, char *, int);
int mystrncmp(char *, char *, int);

int mystrlen(char *s);

int main(int argc, char *argv[])
{

    char dest[]="ABCDEF";
    char source[]="GHIJ";

    mystrncpy(dest,source,4);
    printf("%s\n",dest);

    char s1[]= "ABCD";
    char t1[]= "EFGHIJ";
    char *d;

    /* We store the result in a new string d */

    if ((d = (char *) malloc(sizeof(char) * (strlen(s1) + + 4 + 1))) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }

    mystrncat(s1,t1,d, 4);
    printf("%s\n", d); /* ABCDEFGH */

    free(d);

    char s2[]= "ABCDEF";
    char t2[]= "ABC";
    int result;

    result = mystrncmp(s2,t2,3);

    printf("%d\n", result);


    return 0;
}

void mystrncat(char *str1, char *str2, char *dest, int n) {
    while(*str1) {
        *dest++ = *str1++;
    }

    while(n-- >0) {
        *dest++ = *str2++;
    }

    *dest = '\0';
}

void mystrncpy(char *dest,char *source,int n)
{
    while(*source && n-- > 0)
        *dest++ = *source++;

    int extra = mystrnlen(dest) - n;

    while (extra-- > 0) {
        *dest++;
    }

    *dest = '\0';
}

/* mystrcmp: return <0 if s <t , 0 if s==t, > 0 if s > t */
int mystrncmp(char *lhs,char *rhs, int n)
{
    for(; *lhs == *rhs; lhs++,rhs++)
        if( *lhs == '\0' || --n <=0)
            return 0;
    return *lhs - *rhs;

}


int mystrnlen(char *s) {
    char *p = s;
    while (*s != '\0') {
        s = s + 1;
    }
    return s - p;
}
Run this

Explanation

mystrlen assigns the address of s to p in char *p = s and then goes one character at a time, till it reaches 0. When it is at the end of the word, it subtracts the current address s with intial address p, which thus returns the len of string.

mystrncpy copies n characters of source string to destination. It does this by copying or overwriting one character a time from source to destination and keeps track of count n. When source is exhausted or n characters are copied, it checks if there further characters in destination, if it exists, it goes past them without over-writing and then closes the string by 0.

mystrncat, takes three arguments, str1, str2 and dest. It concatenates n characters from str2 to str1 into a new string dest. It does this by copying all characters from str1 to dest and then keeps a track of count n, and copies n characters of str2 to dest. After copying n characters, it closes the dest string by 0 character.

mystrncmp, compares the lhs string with rhs string. It compares one character at a time and as long as both characters are same, it keeps going and if the lhs is exhaused before n characters are compared, it means we still satisfy the criteria and we return 0. Otherwise, it returns the difference between lhs character and rhs character, which will be 0 if they are equal, negative if lhs is smaller than rhs or positive value if lhs is greater than rhs.

Comments by Disqus