Open In App

C Library – <string.h>

Improve
Improve
Like Article
Like
Save
Share
Report

string.h is a standard header file in the C language that contains functions for manipulating strings (arrays of characters). <string.h> header file contains some useful string functions that can be directly used in a program by invoking the #include preprocessor directive.

Syntax:

#include <string.h>

Example:

C




//    C program to demonstrate the use of C string.h
//header file
#include <stdio.h>
#include <string.h>
 
int main()
{
    // initializing some strings
    char str1[20] = "Geeksfor";
    char str2[20] = "Geeks";
 
    // using strlen(), strcat()
    printf("Str1: %s\n", str1);
    printf("Length of Str1 before concatenation: %d\n",
           strlen(str1));
    strcat(str1, str2); // concatenating str1 and str2
    printf("Str1: %s\n", str1);
 
    return 0;
}


Output

Str1: Geeksfor
Length of Str1 before concatenation: 8
Str1: GeeksforGeeks

C string.h Library Functions

<string.h> header file contains the following functions: 

Function Name

Function 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.

Example:

C




// C program to demonstrate the use of different functions
// of string.h library
#include <stdio.h>
#include <string.h>
 
// defining the common size of the string arrays
#define size 50
 
int main()
{
    char destString[size] = "Geeksfor";
    char sourceString[size] = "Geeks";
    char tempDestString[size];
 
    printf("Length of Destination String: %d\n",
           strlen(destString));
 
    // copying sourceString to tempDestString using strcpy()
    strcpy(tempDestString, sourceString);
    printf("tempDestString after strcpy(): %s\n",
           tempDestString);
 
    // concatenating source to destination using strcat()
    strcat(destString, sourceString);
    printf("destString after Concatenation: %s\n",
           destString);
 
    // comparison using strcmp()
    printf("Comparing destString with sourceString: %d\n",
           strcmp(destString, sourceString));
 
    printf("Comparing first 5 characters: %d\n",
           strncmp(destString, sourceString, 5));
 
    // searching substring using strstr()
    printf("Searching sourceString in destString: %s\n",
           strstr(destString, sourceString));
 
    return 0;
}


Output

Length of Destination String: 8
tempDestString after strcpy(): Geeks
destString after Concatenation: GeeksforGeeks
Comparing destString with sourceString: 102
Comparing first 5 characters: 0
Searching sourceString in destString: GeeksforGeeks


Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads