Scroll Progress Bar

String Library Functions

In C language, strings are represented as arrays of characters, terminated by a null character ('\0'). The C Standard Library provides several string manipulation functions to work with strings. Here, I'll explain some commonly used string library functions along with sample code, programming comments, and sample output.

Name Description
strlen() Returns the length of the string
strcpy() Copy one string to another
strncpy() Copy first n characters of one string to another
strcat() Concatenates two strings
strncat() Concatenates first n characters of one string to another
strcmp() Compares two strings
strncmp() Compares first n characters of two strings
strchr() Find the first occurrence of the given character in the string
strrchr() Finds the last occurrence of the given characters in the string
strstr() Find the given substring in the string
strcspn() Returns the span of the source string not containing any character of the given string
strspn() Returns the span of the source string containing only the characters of the given string
strpbrk() Finds the first occurrence of any of the characters of the given string in the source string
strtok() Split the given string into tokens based on some character as a delimiter
strcoll() Compares two strings that are passed
memset() Initialize a block of memory with the given character
memcmp() Compares two blocks of memory
memcpy() Copy two blocks of memory
memmove() Moves two blocks of memory
memchr() Finds the given character in the block of memory
strlen function:

Explanation:The strlen function is used to calculate the length of a given string (the number of characters before the null terminator).

Usage:size_t strlen(const char* str);

Sample Code with Explanation and Output:

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

int main() {
    char message[] = "Hello, World!";
    size_t length = strlen(message);
    printf("Length of the string: %zu\n", length);
    return 0;
}
Output:

Length of the string: 13
strcpy function:

Explanation:The strcpy function is used to copy the contents of one string to another.

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

Sample Code with Explanation and Output:

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];
    strcpy(destination, source);
    printf("Copied string: %s\n", destination);
    return 0;
}
Output:

Copied string: Hello, World!
strcmp function:

Explanation:The strcmp function is used to compare two strings lexicographically.

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

Sample Code with Explanation and Output:

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

int main() {
    char str1[] = "apple";
    char str2[] = "banana";
    int result = strcmp(str1, str2);
    if (result < 0) {
        printf("%s comes before %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s comes after %s\n", str1, str2);
    } else {
        printf("%s is equal to %s\n", str1, str2);
    }
    return 0;
}

Output:

apple comes before banana
strcat function:

Explanation:The strcat function is used to concatenate (append) one string to the end of another string.

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

Sample Code with Explanation and Output:

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

int main() {
    char str1[30] = "Hello";
    char str2[] = ", World!";
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);
    return 0;
}

Output:

Concatenated string: Hello, World!
strchr function:

Explanation:The strchr function is used to find the first occurrence of a specific character in a string.

Usage:char* strchr(const char* str, int character);

Sample Code with Explanation and Output:

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

int main() {
    char str[] = "Hello, World!";
    char character = 'W';
    char* result = strchr(str, character);
    if (result != NULL) {
        printf("Character '%c' found at position: %ld\n", character, result - str);
    } else {
        printf("Character '%c' not found.\n", character);
    }
    return 0;
}

Output:

Character 'W' found at position: 7
strstr function:

Explanation:The strstr function is used to find the first occurrence of a substring in a string.

Usage:char* strstr(const char* str, const char* substr);

Sample Code with Explanation and Output:

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

int main() {
    char str[] = "Hello, World!";
    char substr[] = "World";
    char* result = strstr(str, substr);
    if (result != NULL) {
        printf("Substring '%s' found at position: %ld\n", substr, result - str);
    } else {
        printf("Substring '%s' not found.\n", substr);
    }
    return 0;
}

Output:

Substring 'World' found at position: 7

These are some commonly used string library functions in C, along with sample code, programming comments, and sample outputs. They are essential for various string manipulation tasks in C programming.


What C library function is used to find the length of a string?


strlen

Which function is used to concatenate two strings in C?


strcat

What C function is used to copy one string to another?


strcpy

Which function is used to compare two strings in C for equality?


strcmp

What function in C is used to find the first occurrence of a substring in a string?


strstr