C <string.h> library


The <string.h> header file in C is part of the C Standard Library and provides a set of functions for manipulating C strings (arrays of characters) and performing various operations on them. These functions include string handling, searching, and comparison.

Key Features of <string.h>

  1. String Manipulation: Functions for copying, concatenating, and comparing strings.
  2. Searching and Tokenizing: Functions for searching substrings and splitting strings into tokens.
  3. Length and Modification: Functions for determining the length of strings and modifying them.

Commonly Used Functions in <string.h>

Here are some of the most commonly used functions provided by <string.h>:

1. String Length and Comparison Functions

  • strlen(): Returns the length of a string (excluding the null terminator).

    Syntax:

    size_t strlen(const char *str);

    Example:

    char str[] = "Hello, World!"; size_t length = strlen(str); // length = 13
  • strcmp(): Compares two strings lexicographically.

    Syntax:

    int strcmp(const char *str1, const char *str2);

    Example:

    int result = strcmp("abc", "def"); // result < 0 (because "abc" < "def")
  • strncmp(): Compares the first n characters of two strings.

    Syntax:

    int strncmp(const char *str1, const char *str2, size_t n);

    Example:

    int result = strncmp("abc", "abc123", 3); // result = 0 (first 3 characters are equal)

2. String Copying and Concatenation Functions

  • strcpy(): Copies one string to another.

    Syntax:

    char *strcpy(char *dest, const char *src);

    Example:

    char src[] = "Hello"; char dest[10]; strcpy(dest, src); // dest = "Hello"
  • strncpy(): Copies up to n characters from one string to another.

    Syntax:

    char *strncpy(char *dest, const char *src, size_t n);

    Example:

    char src[] = "Hello, World!"; char dest[10]; strncpy(dest, src, 5); // dest = "Hello" dest[5] = '\0'; // Null-terminate the string
  • strcat(): Concatenates two strings (appends the source string to the destination string).

    Syntax:

    char *strcat(char *dest, const char *src);

    Example:

    char dest[20] = "Hello"; strcat(dest, " World!"); // dest = "Hello World!"
  • strncat(): Concatenates up to n characters from one string to another.

    Syntax:

    char *strncat(char *dest, const char *src, size_t n);

    Example:

    char dest[20] = "Hello"; strncat(dest, " World!", 5); // dest = "Hello World"

3. Searching and Tokenizing Functions

  • strchr(): Searches for the first occurrence of a character in a string.

    Syntax:

    char *strchr(const char *str, int c);

    Example:

    char *ptr = strchr("Hello, World!", 'W'); // ptr points to "World!"
  • strstr(): Searches for the first occurrence of a substring in a string.

    Syntax:

    char *strstr(const char *haystack, const char *needle);

    Example:

    char *ptr = strstr("Hello, World!", "World"); // ptr points to "World!"
  • strtok(): Tokenizes a string into a sequence of tokens based on specified delimiters.

    Syntax:

    char *strtok(char *str, const char *delim);

    Example:

    char str[] = "Hello, World!"; char *token = strtok(str, ", "); // token = "Hello"

4. Memory and Other Functions

  • memset(): Fills a block of memory with a specified value.

    Syntax:

    void *memset(void *ptr, int value, size_t num);

    Example:

    char buffer[10]; memset(buffer, 'A', sizeof(buffer)); // Fills buffer with 'A'
  • memcpy(): Copies a block of memory from one location to another.

    Syntax:

    void *memcpy(void *dest, const void *src, size_t num);

    Example:

    char src[] = "Hello"; char dest[10]; memcpy(dest, src, strlen(src) + 1); // Copies "Hello" including null terminator
  • memmove(): Similar to memcpy(), but safer for overlapping memory regions.

    Syntax:

    void *memmove(void *dest, const void *src, size_t num);

    Example:

    char str[] = "Hello, World!"; memmove(str + 7, str, 5); // str = "Hello, Hello!"

Using <string.h>

To use the functions defined in <string.h>, include the header file at the beginning of your C source file:

#include <string.h>

Summary

  • <string.h> provides essential functions for manipulating C strings.
  • Functions like strlen(), strcmp(), and strcpy() are crucial for string handling.
  • Searching functions like strchr() and strstr() facilitate substring searches.
  • Memory manipulation functions such as memset(), memcpy(), and memmove() are also included.
  • Understanding and effectively using <string.h> is vital for working with strings in C programming.