Explain string library functions with suitable examples in C

The C standard library provides several predefined functions for string manipulation in the string.h header file. These functions help perform operations like copying, comparing, concatenating, and searching strings efficiently.

Common String Functions

The strlen() Function

The strlen() function returns the number of characters in a string, excluding the null terminator.

Syntax

size_t strlen(const char *str);

Example

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

int main() {
    char a[30] = "Hello";
    int l;
    l = strlen(a);
    printf("Length of the string = %d<br>", l);
    return 0;
}
Length of the string = 5

The strcpy() Function

The strcpy() function copies the source string into the destination string. The destination must have enough space to hold the source string.

Syntax

char *strcpy(char *destination, const char *source);

Example

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

int main() {
    char source[50] = "TutorialsPoint";
    char destination[50];
    strcpy(destination, source);
    printf("Source string: %s<br>", source);
    printf("Copied string: %s<br>", destination);
    return 0;
}
Source string: TutorialsPoint
Copied string: TutorialsPoint

The strcat() Function

The strcat() function concatenates (joins) two strings. It appends the source string to the end of the destination string.

Syntax

char *strcat(char *destination, const char *source);

Example

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

int main() {
    char a[50] = "Hello ";
    char b[20] = "World";
    strcat(a, b);
    printf("Concatenated string = %s<br>", a);
    return 0;
}
Concatenated string = Hello World

The strcmp() Function

The strcmp() function compares two strings lexicographically. It returns 0 if strings are equal, positive value if first string is greater, and negative value if first string is smaller.

Syntax

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

Example

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

int main() {
    char str1[] = "apple";
    char str2[] = "ball";
    int result = strcmp(str1, str2);
    
    if (result == 0) {
        printf("%s is equal to %s<br>", str1, str2);
    } else if (result < 0) {
        printf("%s is less than %s<br>", str1, str2);
    } else {
        printf("%s is greater than %s<br>", str1, str2);
    }
    return 0;
}
apple is less than ball

The strstr() Function

The strstr() function searches for the first occurrence of a substring within a main string. It returns a pointer to the first occurrence or NULL if not found.

Syntax

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

Example

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

int main() {
    char mainStr[] = "Hello TutorialsPoint";
    char searchStr[] = "Tutorials";
    char *found = strstr(mainStr, searchStr);
    
    if (found) {
        printf("'%s' found at position %ld<br>", searchStr, found - mainStr);
    } else {
        printf("'%s' not found<br>", searchStr);
    }
    return 0;
}
'Tutorials' found at position 6

Conclusion

String library functions in C provide essential operations for string manipulation. These functions help handle copying, concatenation, comparison, and searching operations efficiently while maintaining proper memory management.

Updated on: 2026-03-15T13:31:32+05:30

41K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements