Basic string functions

Implement basic string manipulation functions such as - strchr, strcmp, strcpy, strcat, etc.
/*
 * StringFuncs.cpp
 *
 *  Created on: Apr 9, 2012
 *      Author: Kiran Hegde
 */

#include <string.h>
#include <iostream>

using namespace std;

/**
 * The following functions are based on public domain implementation
 */

/* strchr */
char *mystrchr(const char *s, int c) {
    /* Scan s for the character.  When this loop is finished,
  s will either point to the end of the string or the
  character we were looking for.  */
    while (*s != '\0' && *s != (char) c)
        s++;
    return ((*s == c) ? (char *) s : NULL);
}

/* strcmp */
int mystrcmp(const char *s1, const char *s2) {
    unsigned char uc1, uc2;
    /* Move s1 and s2 to the first differing characters
  in each string, or the ends of the strings if they
  are identical.  */
    while (*s1 != '\0' && *s1 == *s2) {
        s1++;
        s2++;
    }
    /* Compare the characters as unsigned char and
  return the difference.  */
    uc1 = (unsigned char) *s1;
    uc2 = (unsigned char) *s2;
    return ((uc1 < uc2) ? -1 : (uc1 > uc2));
}

/* strcpy */
char *mystrcpy(char *s1, const char *s2) {
    char *dst = s1;
    const char *src = s2;
    /* Do the copying in a loop.  */
    while ((*dst++ = *src++) != '\0')
        ; /* The body of this loop is left empty. */
    /* Return the destination string.  */
    return s1;
}

/* strncat */
char *mystrncat(char *s1, const char *s2, size_t n) {
    char *s = s1;
    /* Loop over the data in s1.  */
    while (*s != '\0')
        s++;
    /* s now points to s1's trailing null character, now copy
  up to n bytes from s1 into s stopping if a null character
  is encountered in s2.
  It is not safe to use strncpy here since it copies EXACTLY n
  characters, NULL padding if necessary.  */
    while (n != 0 && (*s = *s2++) != '\0') {
        n--;
        s++;
    }
    if (*s != '\0')
        *s = '\0';
    return s1;
}

/* strncmp */
int mystrncmp(const char *s1, const char *s2, size_t n) {
    unsigned char uc1, uc2;
    /* Nothing to compare?  Return zero.  */
    if (n == 0)
        return 0;
    /* Loop, comparing bytes.  */
    while (n-- > 0 && *s1 == *s2) {
        /* If we've run out of bytes or hit a null, return zero
  since we already know *s1 == *s2.  */
        if (n == 0 || *s1 == '\0')
            return 0;
        s1++;
        s2++;
    }
    uc1 = (unsigned char) *s1;
    uc2 = (unsigned char) *s2;
    return ((uc1 < uc2) ? -1 : (uc1 > uc2));
}

/* strncpy */
char *mystrncpy(char *s1, const char *s2, size_t n) {
    char *dst = s1;
    const char *src = s2;
    /* Copy bytes, one at a time.  */
    while (n > 0) {
        n--;
        if ((*dst++ = *src++) == '\0') {
            /* If we get here, we found a null character at the end
  of s2, so use memset to put null bytes at the end of
  s1.  */
            memset(dst, '\0', n);
            break;
        }
    }
    return s1;
}

/* strrchr */
char *mystrrchr(const char *s, int c) {
    const char *last = NULL;
    /* If the character we're looking for is the terminating null,
  we just need to look for that character as there's only one
  of them in the string.  */
    if (c == '\0')
        return strchr(s, c);
    /* Loop through, finding the last match before hitting NULL.  */
    while ((s = strchr(s, c)) != NULL) {
        last = s;
        s++;
    }
    return (char *) last;
}

/**
 * test the functions
 */
int main() {
    const char *testStr = "hello string";
    cout << "s = " << testStr << endl;
    cout << "mystrchr (s, 'l') = ";
    cout << mystrchr(testStr, 'l') << endl;
    cout << "mystrrchr (s, 'l') = ";
    cout << mystrrchr(testStr, 'l') << endl;

    cout << "mystrcmp (s, 'help')= ";
    cout << mystrcmp(testStr, "help") << endl;
    cout << "mystrcmp (s, 'heck')= ";
    cout << mystrcmp(testStr, "heck") << endl;
    cout << "mystrcmp (s, 'hello string')= ";
    cout << mystrcmp(testStr, "hello string") << endl;

    cout << "mystrncmp (s, 'help', 3)= ";
    cout << mystrncmp(testStr, "help", 3) << endl;
    cout << "mystrncmp (s, 'help', 4)= ";
    cout << mystrncmp(testStr, "help", 4) << endl;
    cout << "mystrncmp (s, 'held', 4)= ";
    cout << mystrncmp(testStr, "held", 4) << endl;

    char temp[256];
    const char *constStr = "some string";
    cout << "s = " << constStr << endl;
    mystrcpy(temp, constStr);
    cout << "After mystrcpy(temp, s), temp = ";
    cout << temp << endl;

    mystrncpy(temp, constStr, 4);
    cout << "After mystrncpy(temp, s, 6) temp = ";
    cout << temp << endl;

    cout << "After mystrncat(temp, s, 20) temp = ";
    mystrncat(temp, testStr, 20);
    cout << temp << endl;
}
Comments