String Handling Functions in C

C provides several functions to manipulate and work with strings, which are stored in the string.h library. Below are some commonly used functions for various operations such as finding the length of a string, copying one string to another, concatenating strings, comparing strings, and searching for substrings.

1. strlen - Finding the Length of a String

The strlen function returns the number of characters in a string, excluding the null character ('0') at the end.

Syntax:

size_t strlen(const char *str);

Example:


#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello, World!";
    printf("Length of the string: %lun", strlen(str));
    return 0;
}

Output

Length of the string: 13

2. strcpy - Copying Strings

The strcpy function copies the contents of one string into another. The destination string must be large enough to hold the source string, including the null character at the end.

Syntax:

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

Example:


#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!

3. strcat - Concatenating Strings

The strcat function appends (concatenates) one string to the end of another. The destination string should have enough space to accommodate both strings and the null character.

Syntax:

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

Example:


#include <stdio.h>
#include <string.h>
int main() {
    char str1[20] = "Hello, ";
    char str2[] = "World!";
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);
    return 0;
}

Output

Concatenated string: Hello, World!

4. strcmp - Comparing Strings

The strcmp function compares two strings lexicographically (character by character). It returns 0 if both strings are equal, a negative value if the first string is less than the second, and a positive value if the first string is greater.

Syntax:

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

Example:


#include <stdio.h>
#include <string.h>
int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("Strings are equal\n");
    } else if (result < 0) {
        printf("str1 is less than str2\n");
    } else {
        printf("str1 is greater than str2\n");
    }
    return 0;
}

Output

str1 is less than str2

5. strchr - Finding a Character in a String

The strchr function searches for the first occurrence of a character in a string and returns a pointer to it. If the character is not found, it returns NULL.

Syntax:

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

Example:


#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello, World!";
    char *ptr = strchr(str, 'W');
    if (ptr != NULL) {
        printf("Character found: %c\n", *ptr);
    } else {
        printf("Character not found\n");
    }
    return 0;
}

Output

Character found: W

6. strstr - Finding a Substring

The strstr function locates the first occurrence of a substring within a string and returns a pointer to it. If the substring is not found, it returns NULL.

Syntax:

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

Example:


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

int main() {
    char str[] = "Hello, World!";
    char *ptr = strstr(str, "World");
    if (ptr != NULL) {
        printf("Substring found: %s\n", ptr);
    } else {
        printf("Substring not found\n");
    }
    return 0;
}
            

Output

Substring found: World!

7. strncpy - Copying a Fixed Number of Characters

The strncpy function copies a specified number of characters from the source string to the destination string. If the length of src is less than n, it pads the destination with null characters.

Syntax:

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

Example:


#include <stdio.h>
#include <string.h>
int main() {
    char source[] = "Hello, World!";
    char destination[10];
    strncpy(destination, source, 5);
    destination[5] = '0'; // Manually add null terminator
    printf("Copied string: %s\n", destination);
    return 0;
}

Output

Copied string: Hello