C Language String Functions: Implementation and Usage Guide

strlen Function

Key Characteristics

  • Strings terminate with '\0', and the function counts characters before this termination marker (excluding '\0')
  • The input string must end with '\0'
  • Returns size_t type, which is unsigned (common source of errors)
  • Requires string.h header inclusion

Basic Usage Example

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

int main()
{
    char buffer[] = "programming";
    size_t length = strlen(buffer);
    printf("%zu\n", length);
    return 0;
}

Common Pitfall with size_t Return Type

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

int main()
{
    if (strlen("test") - strlen("example") > 0)
        printf("Positive\n");
    else
        printf("Negative or zero\n");
    return 0;
}

Output Explanation

Since strlen returns size_t, the subtraction result remains unsigned.

Solution with Type Casting

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

int main()
{
    if ((int)strlen("test") - (int)strlen("example") > 0)
        printf("Positive\n");
    else
        printf("Negative or zero\n");
    return 0;
}

Custom strlen Implementation

Method One: Counter Approach

size_t custom_strlen(const char* ptr)
{
    assert(ptr);
    size_t counter = 0;
    while (*ptr)
    {
        counter++;
        ptr++;
    }
    return counter;
}

Method Two: Pointer Arithmetic

size_t custom_strlen(const char* ptr)
{
    assert(ptr);
    const char* initial = ptr;
    while (*ptr)
    {
        ptr++;
    }
    return ptr - initial;
}

Method Three: Recursive Implementation

size_t custom_strlen(const char* ptr)
{
    assert(ptr);
    if (*ptr == '\0')
        return 0;
    else
        return 1 + custom_strlen(ptr + 1);
}

strcpy Function

Important Considerations

  • Source string must end with '\0'
  • Copies the null terminator to destination
  • Destination buffer must accommodate the entire source string
  • Destination memory must be writable

Basic Usage

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

int main()
{
    char target[15] = {0};
    char source[] = "hello world";
    strcpy(target, source);
    printf("%s\n", target);
    return 0;
}

Custom strcpy Implementation

char* custom_strcpy(char* destination, const char* source)
{
    assert(destination);
    assert(source);
    
    char* result = destination;
    while (*source)
    {
        *destination = *source;
        destination++;
        source++;
    }
    *destination = *source;
    return result;
}

strcat Functon

Critical Points

  • Source string must end with '\0'
  • Destination string must contain '\0' to determine append location
  • Destination buffer must be large enough for both strings
  • Destination memory must be modifiable

Usage Example

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

int main()
{
    char first[30] = "Hello ";
    char second[] = "World!";
    strcat(first, second);
    printf("%s\n", first);
    return 0;
}

Custom strcat Implementation

char* custom_strcat(char* destination, const char* source)
{
    char* result = destination;
    assert(destination);
    assert(source);
    
    while (*destination)
    {
        destination++;
    }
    while (*destination++ = *source++);
    return result;
}

strcmp Function

Return Values

  • First string greater than second → positive number
  • Strings equal → zero
  • First string less than second → negative number

The comparison uses ASCII values of corresponding characters.

Implementation Example

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

int main()
{
    char text1[] = "apple";
    char text2[] = "banana";
    int comparison = strcmp(text1, text2);
    printf("%d\n", comparison);
    return 0;
}

Custom strcmp Implementation

int custom_strcmp(const char* first, const char* second)
{
    while (*first == *second)
    {
        first++;
        second++;
        if (*first == '\0')
            return 0;
    }
    if (*first > *second) return 1;
    if (*first < *second) return -1;
    return 0; // This line handles all cases
}

strstr Function

Return Value

Returns the position where the second string first appears in the first string. Returns NULL if no match is found.

Usage Example

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

int main()
{
    char haystack[] = "abcdefghijk";
    char needle[] = "fgh";
    char* found = strstr(haystack, needle);
    if (found != NULL)
        printf("Found: %s\n", found);
    else
        printf("Not found\n");
    return 0;
}

Custom strstr Implementation

char* custom_strstr(const char* haystack, const char* needle)
{
    const char* current = haystack;
    const char* h_ptr = NULL;
    const char* n_ptr = NULL;
    assert(haystack && needle);
    
    if (*needle == '\0') return (char*)haystack;
    
    while (*current)
    {
        h_ptr = current;
        n_ptr = needle;
        while (*h_ptr && *n_ptr && *h_ptr == *n_ptr)
        {
            h_ptr++;
            n_ptr++;
        }
        if (*n_ptr == '\0') return (char*)current;
        current++;
    }
    return NULL;
}

Tags: c programming String Functions strlen strcpy strcat

Posted on Fri, 14 Aug 2026 16:33:59 +0000 by Rangana