C programming string handling library functions


In this article, you will learn about different types of C string handling library functions that are used for string manipulation.

C string handling library functions

All of the standard string manipulation functions of C programming are defined in string handling library string.h. Therefore we should include string library before using built-in functions.

#include<string.h>

These functions can be used to carry out many string manipulation such as copying the string, concatenating strings, comparing strings, searching strings for characters and determining the length of strings.

Anthony J. D’Angelo once said

“Don’t reinvent the wheel, just realign it”

We should also use C standard library function whenever possible instead of writing new functions.

C string handling library functions  <string.h>


String handling functions with description
strcpy( str1, str2 );
Copies string str2 into character array str1
strncpy( str1, str2, n );
Copies at most n characters of str2 into character array str1.
strcat( str1, str2 );
Appends string str2 into character array str1
strncat( str1, str2, n );
Appends at most n characters of str2 into character array str1.
strcmp( str1, str2 );
Compares the string str1 with the string str2.
strncmp( str1, str2, n );
Compares up to n characters of the string str1 with the string str2.
strchr( str, ch );
Locates the character ch in the string str.
strcspn( str1, str2);
Calculates the length of the first segment of string str1 consisting of characters that are not present in the string str2.
strspn( str1, str2);
Calculates the length of the first segment of string str1 consisting only of characters that are present in the string str2.
strpbrk( str1, str2);
Locates the first occurrence of any character of string str2 into the string str1.
strrchr( str, ch );
Locates the last occurrence of character ch in the string str.
strstr( str1, str2 );
Locates the first occurrence of the string str2 into the string str1.
strlen( str );
Calculates the length of the string str.
strerror( errornum );
Displays the error message.

Memory functions of the C programming string handling library


The following listed functions of string handling library can manipulate, compare and search the character arrays that are treated as a block of memory.

Therefore these functions can manipulate any block of data.

Memory related function of string handling library with description
memcpy(  str1, str2, n );
Copies n characters from the block of data pointed to by str2 into str1.
memmove(  str1, str2, n );
Moves n characters from the block of data pointed to by str2 into str1.
memcmp( str1, str2, n );
Compares the first n characters from the block of data pointed to by str2 and str1.
memchr( str, ch, n );
Locates the first occurrence of character ch in the first n characters of the block of data pointed to by str.
memset( str, ch, n );
Copies ch into the first n characters of the block of data pointed by str.

C Programming String Conversion Functions

In C programming string conversion functions are defined under stdlib.h library. String conversion function in C converts strings of digits to integers and floating-point values.

Check out three different string conversion function in C programming.

C programming string conversion functions with description
strtod(  sPtr, &endPtr );
Converts the digits of string sPtr to double.
strtol(  sPtr, &endPtr, base );
Converts the digits of string sPtr to long.
strtoul( sPtr, &endPtr, base );
Converts the digits of string sPtr to unsingned long.