C++ Notes: Common <cstring> functions

Common <cstring> functions

Here are a number of useful functions work with character arrays (C-strings). The types of the parameters are cs=char[] or char*, c=char/int. Where true appears, zero is false and non-zero is true.

TypeMethodDescription
#include <cstring> (or older <string.h>)
intstrlen(const cs) Returns the length of the c-string cs.
char*strcpy(cs1, const cs2) Copies cs2 to cs1. Returns the first parameter (usually ignored).
char*strcat(cs1,  const cs2) Concatenates cs2 on the end of cs1. Returns the first parameter (usually ignored).
intstrcmp(const cs1, const cs2) Returns one of three kinds of values: negative if cs1 < cs2, zero if cs1 == cs2, or positive if cs1 > cs2.
char*strchr(const csc) Returns a pointer to the first occurrence of c in cs or NULL if it isn't in the string.
char*strstr(const cs1, const cs2) Returns a pointer to the first occurrence of cs2 in cs1 or NULL if it isn't in the string.