C Library – <string.h>
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] = "Beginnerfor" ; char str2[20] = "Beginner" ; // 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: Beginnerfor Length of Str1 before concatenation: 8 Str1: w3wiki
C string.h Library Functions
<string.h> header file contains the following functions:
Function Name |
Function Description |
---|---|
Returns the length of the string. | |
Copy one string to another. | |
strncpy() |
Copy first n characters of one string to another. |
Concatenates two strings. | |
Concatenates first n characters of one string to another. | |
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. |
Find the given substring in the string. | |
Returns the span of the source string not containing any character of the given string. | |
Returns the span of the source string containing only the characters of the given string. | |
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. |
Initialize a block of memory with the given character. | |
memcmp() |
Compares two blocks of memory. |
Copy two blocks of memory. | |
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] = "Beginnerfor" ; char sourceString[size] = "Beginner" ; 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(): Beginner destString after Concatenation: w3wiki Comparing destString with sourceString: 102 Comparing first 5 characters: 0 Searching sourceString in destString: w3wiki
Contact Us