String handling routines in the C standard library

Learn how the C standard library enables us to manage strings using functions for concatenating strings, finding their lengths, testing for equality, as well as allowing conversions between strings and numbers.

The standard C library, (which we can load into our program by including the statement #include <string.h> at the top of your program), contains many useful routines for manipulating these null-terminated strings.

It’s a good idea to consult a reference source (for example, the Wikipedia article C String Handling) for a list (it’s relatively long) of all the functions that exist for manipulating null-terminated strings in C. There are functions for copying strings, concatenating strings, getting the length of strings, comparing strings, etc.

Concatenating and finding length

We can use the strcat function to concatenate two strings by appending the second string to the first one. We show this below, by passing the string s3 as the first argument in each call to strcat.

We can use the strlen function to find the length of a string in terms of characters that appear before the (first) null character.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy