String Manipulation Functions in C

strcpy - Copy Strings

  • Header: #include <string.h>
  • Syntax: strcpy(dest, src)
  • Purpose: Copies the string src into the character array dest
  • Returns: Pointer to the start of dest
  • Notes:
    • dest must be large enough to hold the copied string
    • The null terminator '\0' is also copied

strcat - Concatenate Strings

  • Header: #include <string.h>
  • Syntax: strcat(dest, src)
  • Purpose: Appends the string src to the end of dest
  • Returns: Pointer to the start of dest
  • Notes:
    • dest must have enough space to accommodate the result
    • The null terminator from dest is removed and a new one added at the end

strcmp - Compare Strings

  • Header: #include <string.h>
  • Syntax: strcmp(str1, str2)
  • Purpose: Compares two strings character by character
  • Returns:
    • Negative if str1 is less than str2
    • Positive if str1 is greater than str2
    • Zero if they are equal
  • Comparison is based on ASCII values until a mismatch or null terminator is found

isalpha - Check Alphabetic Characters

  • Header: #include <ctype.h>
  • Syntax: isalpha(c)
  • Purpose: Checks if a character is an alphabetic letter
  • Returns:
    • Non-zero if it's a letter
    • Zero otherwise

isupper - Check Uppercase Letters

  • Header: #include <ctype.h>
  • Syntax: isupper(c)
  • Purpose: Determines if a character is an uppercase letter
  • Returns:
    • Non-zero if it's uppercase
    • Zero otherwise

toupper - Convert to Uppercase

  • Header: #include <ctype.h>
  • Syntax: toupper(c)
  • Purpose: Converts a lowercase character to uppercase
  • Returns:
    • Uppercase version if input is lowercase
    • Original character otherwise

tolower - Convert to Lowercase

  • Header: #include <ctype.h>
  • Syntax: tolower(c)
  • Purpose: Converts an uppercase character to lowercase
  • Returns:
    • Lowercase version if enput is uppercase
    • Original character otherwise

strncpy - Copy Fixed-Length Strings

  • Header: #include <string.h>
  • Syntax: strncpy(dest, src, n)
  • Purpose: Copies up to n characters from src to dest
  • Notes:
    • If src is shorter than n, the rest of dest is padded with nulls
    • If src is longer than or equal to n, dest won't be null-terminated

strncat - Concatenate Limited Strings

  • Header: #include <string.h>
  • Syntax: strncat(dest, src, n)
  • Purpose: Appends up to n characters from src to dest
  • Notes:
    • The null terminator is preserved at the end of the result
    • If src is shorter than n, only its contents are appended

strncmp - Compare Limited Strings

  • Header: #include <string.h>
  • Syntax: strncmp(str1, str2, n)
  • Purpose: Compares up to n characters from two strings
  • Returns:
    • Zero if the first n characters are equal
    • Positive or negative depending on the first differing character

strchr - Find Character in String

  • Header: #include <string.h>
  • Syntax: strchr(str, c)
  • Purpose: Finds the first occurrence of a character in a string
  • Returns:
    • Pointer to the first occurrence of c
    • NULL if not found

Custom Implementation of strchr

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

strstr - Find Substring in String

  • Header: #include <string.h>
  • Syntax: strstr(str1, str2)
  • Purpose: Locates the first occurrence of a substring within a string
  • Returns:
    • Pointer to the start of the substring
    • NULL if not found

Custom Implementation of strstr

char* my_strstr(const char* str1, const char* str2) {
    const char* p = str1;
    while (*p != '\0') {
        const char* s1 = p;
        const char* s2 = str2;
        while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) {
            s1++;
            s2++;
        }
        if (*s2 == '\0') return (char*)p;
        p++;
    }
    return NULL;
}

Tags: C string.h ctype.h string-manipulation

Posted on Wed, 20 May 2026 18:26:33 +0000 by guayaquil