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

int value = 10;
int *int_ptr = & value;
some_function(int_ptr);
printf("%d\n", value);

void some_function(int* some_ptr)
{
    *some_ptr = 11;
}

4.Reassignment of pointers

int value = 10;
int *int_ptr = & value;
some_function(int_ptr);
printf("%d\n", value);

void some_function(int* some_ptr)
{
    some_ptr = (int*) malloc (sizeof(int)); 
    *some_ptr = 11;
}

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

char* my_strstr(const char* heystack, const char* needle)
{    
    if (heyStack == NULL || needle == NULL)
    {
        return NULL;
    }
    while (*heystack != '\0')
    {
        if (*heystack == *needle)
        {
            const char* h = heystack;
            const char* n = needle
            while (*n != '\0' && *h == *n)
            {
                h++;
                n++;
            }
            if (*n == '\0')
            {
                return (char*) heyStack;
            }
        }
        heystack++;
    }
    return NULL;
}

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

int my_strcmp(const char* str1, const char* str2)
{
    if (str1 == NULL && str2 == NULL)
    {
        return 0;
    }
    if (str1 == NULL)
    {
        return 0 - *str2;
    }
    if (str2 == NULL)
    {
        return *str1;
    }
    
    const char* ch1 = str1;
    const char* ch2 = str2;
    
    while (*ch1 == '\0' && *ch2 == '\0')
    {
        if (*ch1 != *ch2)
        {
            return *ch1 - *ch2;
        }
        ch1++;
        ch2++;
    }
    
    if (*ch1 == '\0' && *ch2 != '\0')
    {
        return 0 - *str2;
    }
    if (*ch1 != '\0' && *ch2 == '\0')
    {
        return *str1;
    }
}

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

char* my_strcat(char* dest, const char* src)
{    
    if (dest == NULL || src == NULL)
    {
        return dest;
    }
    chasr* d = dest;
    while (*d != '\0')
    {
        d++;
    }
    while (*src != '\0')
    {
        *d = *src;
        d++;
        src++;
    }
    *d = '\0';
    return NULL;
}

Last updated