Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

D.5 The string.h Module

#include <string.h>

This module defines a collection of functions for working with strings and arrays.

Functions

Length Function
int strlen(char * str);
Returns the length of the given string.
Copy/Append Functions
char * strcpy(char * dest, const char *src);
Copies the src string to the dest character array, character by character.
char * strncpy(char * dest, const char *src, int num);
Copies the first num characters of src string to the dest character array.
char * strcat(char * dest, const char *src);
Appends a copy of the src string to the dest string.
char * strncat(char * dest, const char *src, int num);
Appends the first num characters of the src string to the dest string.
String Comparison Functions
Searching Functions
Memory/Array Functions

Detailed Description

int strlen(char * str);
Returns the length of the given string. The length of the string includes all characters from the first position of the array up to, but not including the null-terminating character.
Parameters:
str
A pointer to a character array that contains a C string.
Returns:
The length of the string.
char * strcpy(char * dest, const char *src);
Copies the src string to the dest character array, character by character. The entire string is copied from the src array to the dest array, up to and including the null terminating character.
Parameters:
dest
A pointer to the destination character array where the contents of src are copied. The destination array must be large enough to hold the source string, including the null terminating character.
src
A pointer to the character array containing the source C string to be copied.
Returns:
The pointer to the dest array.
See Also:
strncpy()
char * strncpy(char * dest, const char *src, int num);
Copies the first num characters of src string to the dest character array. If the source string contains less than num characters, the destination is padded with zeroes for a total of num characters, otherwise, a single null character is appended to the end of the destination.
Parameters:
dest
A pointer to the destination character array where the contents of src are copied. The destination array must be large enough to hold the entire source string, including the null terminating character.
src
A pointer to the character array containing the source string to be copied.
num
The maximum number of characters to be copied from the source to the destination.
Returns:
The pointer to the dest array.
See Also:
strncat()
char * strcat(char * dest, const char *src);
Appends a copy of the src string to the dest string. The null terminating character in the destination array is replaced by the first character in the source array and a null-character is included at the end of the new concatenated string.
Parameters:
dest
A pointer to the destination character array, which must contain a C string and be large enough to hold the resulting concatenated string.
src
A pointer to the character array containing the source string to be appended to the destination string.
Returns:
The pointer to the dest array.
See Also:
strncat()
char * strncat(char * dest, const char *src, int num);
Appends the first num characters of the src string to the dest string. The null terminating character in the destination array is replaced by the first character in the source array and a null-character is included at the end of the new concatenated string. If the source string contains less than num characters, only the characters up to the null terminating character are copied.
Parameters:
dest
A pointer to the destination character array, which must contain a C string and be large enough to hold the resulting concatenated string, including the null-character.
src
A pointer to the character array containing the source string to be appended to the destination string.
num
The maximum number of characters copied from the source string to the destination.
Returns:
The pointer to the dest array.
See Also:
strcat()