C string.h Library: Functions for String Manipulation

The <string.h> library in C offers a comprehensive set of functions for manipulating strings and memory blocks. These functions facilitate essential tasks such as copying, concatenation, comparison, and searching, making string handling in C more efficient.



C string.h Library

The <string.h> header provides a variety of functions to manipulate strings and memory blocks. These functions help perform common tasks such as copying, concatenation, comparison, and searching.

Common C string.h Functions

Below is a list of functions provided by the string.h library:

Function Description
memchr() Returns a pointer to the first occurrence of a value in a block of memory.
memcmp() Compares two blocks of memory to determine which represents a larger value.
memcpy() Copies data from one block of memory to another.
memmove() Copies data from one block of memory to another, accounting for memory overlap.
memset() Sets all the bytes in a block of memory to a specified value.
strcat() Appends one string to the end of another.
strchr() Returns a pointer to the first occurrence of a character in a string.
strcmp() Compares two strings by their ASCII values to determine which is greater.
strcoll() Compares two strings based on locale-specific values.
strcpy() Copies a string into another memory location.
strcspn() Returns the length of a string up to the first occurrence of specified characters.
strerror() Returns a string describing an error code.
strlen() Returns the length of a string.
strncat() Appends a specified number of characters from one string to another.
strncmp() Compares the ASCII values of a specified number of characters in two strings.
strncpy() Copies a specified number of characters from one string to another memory location.
strpbrk() Returns a pointer to the first position in a string that contains specified characters.
strrchr() Returns a pointer to the last occurrence of a character in a string.
strspn() Returns the length of a string up to the first character that is not one of the specified characters.
strstr() Returns a pointer to the first occurrence of a substring in another string.
strtok() Splits a string into tokens based on specified delimiters.
strxfrm() Transforms a string based on locale-specific rules.

Example: String Manipulation with string.h

Let’s see an example of some string.h functions in action:

Example: String Concatenation and Comparison

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

int main() {
    char str1[50] = "Hello";
    char str2[50] = "World";

    // Concatenate str2 onto str1
    strcat(str1, " ");
    strcat(str1, str2);

    // Print the concatenated string
    printf("Concatenated String: %s\n", str1);

    // Compare two strings
    int cmp = strcmp(str1, "Hello World");
    if (cmp == 0) {
        printf("Strings are equal!\n");
    } else {
        printf("Strings are not equal!\n");
    }

    return 0;
}

Output:

Output

Concatenated String: Hello World
Strings are equal!

This program concatenates two strings using strcat() and compares them using strcmp(). If the strings are equal, it prints a confirmation message.