String

1.String length implementation

size_t my_strlen(const char* str)
{    
    if (str == NULL)
    {
        return 0;
    }
    int length = 0;
    const char *ch = str;
    while (*ch != '\0')
    {
        length++;
        ch++;
    }
    return length;
}

2.String character check implementation

size_t my_strchar(const char* str, int c)
{    
    if (str == NULL)
    {
        return 0;
    }
    while (*str != '\0')
    {
        if (*str == c)
        {
            return (char*) str;
        }
        str++;
    }
    return NULL;
}

3.Modifications to pointers

4.Reassignment of pointers

5.A function to check for a string in a string

  • Characters are integers at heart, int c is just the ASCII code for the characters and can be tested for equality with characters

6.A function to compare two strings

  • < 0 if str1 is less than str2

  • > 0 if str1 is greater than str2

  • It should be able to handle NULL inputs

  • Character ASCII codes

    • A -> 65, Z -> 90

    • a -> 97, z -> 122

  • 'B' - 'A' = 1

7. A function to concatenate strings

  • Copying can be done character by character

  • Assume there is enough space in the destination string to hold the source appended to it

Last updated