C++ Crashkurs
String functions as provided by string.h
in the C standard library.
More...
Functions | |
char * | strchrnul (const char *s, int c) |
Find the first occurrence of a character in a string. More... | |
char * | strchr (const char *s, int c) |
Find the first occurrence of a character in a string. More... | |
int | strcmp (const char *s1, const char *s2) |
Compare two strings. More... | |
int | strncmp (const char *s1, const char *s2, size_t n) |
Compare two strings. More... | |
size_t | strlen (const char *s) |
Calculate the length of a string. More... | |
size_t | strnlen (const char *s, size_t maxlen) |
Calculate the length of a string, limited by maxlen. More... | |
char * | strcpy (char *dest, const char *src) |
Copy the contents of a string including the terminating null byte (\0 ) More... | |
char * | strncpy (char *dest, const char *src, size_t n) |
Copy the contents of a string up to a maximum length or the terminating null byte (\0 ), whatever comes first. More... | |
char * | strdup (const char *s) |
Duplicate a string. More... | |
char * | strndup (const char *s, size_t n) |
Duplicate a string. More... | |
void * | memcpy (void *__restrict__ dest, void const *__restrict__ src, size_t size) |
Copy a memory area. More... | |
void * | memmove (void *dest, void const *src, size_t size) |
Copy a memory area while the source may overlap with the destination. More... | |
void * | memset (void *dest, int pattern, size_t size) |
Fill a memory area with a pattern. More... | |
int | memcmp (const void *s1, const void *s2, size_t n) |
Compare a memory area. More... | |
Detailed Description
String functions as provided by string.h
in the C standard library.
Function Documentation
char * strchrnul | ( | const char * | s, |
int | c | ||
) |
Find the first occurrence of a character in a string.
- Parameters
-
s string to c character to find
- Returns
- Pointer to first occurrence of the character or to null byte at the end of the string if not found
char * strchr | ( | const char * | s, |
int | c | ||
) |
Find the first occurrence of a character in a string.
- Parameters
-
s string to c character to find
- Returns
- Pointer to first occurrence of the character or to nullptr if not found
int strcmp | ( | const char * | s1, |
const char * | s2 | ||
) |
Compare two strings.
- Parameters
-
s1 first string s2 second string
- Returns
- an integer less than, equal to, or greater than zero if first string is found, respectively, to be less than, to match, or be greater than second string
int strncmp | ( | const char * | s1, |
const char * | s2, | ||
size_t | n | ||
) |
Compare two strings.
- Parameters
-
s1 first string s2 second string n number of bytes to compare
- Returns
- an integer less than, equal to, or greater than zero if the given number of bytes of the first string are found, respectively, to be less than, to match, or be greater than second string
size_t strlen | ( | const char * | s | ) |
Calculate the length of a string.
- Parameters
-
s pointer to a string
- Returns
- number of bytes in the string
size_t strnlen | ( | const char * | s, |
size_t | maxlen | ||
) |
Calculate the length of a string, limited by maxlen.
- Parameters
-
s pointer to a string maxlen upper limit of length to be returned
- Returns
- number of bytes in the string, or maxlen – whichever is smaller
char * strcpy | ( | char * | dest, |
const char * | src | ||
) |
Copy the contents of a string including the terminating null byte (\0
)
- Parameters
-
dest destination string buffer src source string buffer
- Returns
- a pointer to the destination string buffer
- Note
- Beware of buffer overruns!
char * strncpy | ( | char * | dest, |
const char * | src, | ||
size_t | n | ||
) |
Copy the contents of a string up to a maximum length or the terminating null byte (\0
), whatever comes first.
- Parameters
-
dest destination string buffer src source string buffer n maximum number of bytes to copy
- Returns
- a pointer to the destination string buffer
- Note
- If there is no null byte (
\0
) among the firstn
bytes, the destination will not be null-terminated!
char * strdup | ( | const char * | s | ) |
Duplicate a string.
- Parameters
-
s pointer to a string
- Returns
- pointer to a duplicated string allocated with malloc or
nullptr
if allocation failed.
char * strndup | ( | const char * | s, |
size_t | n | ||
) |
Duplicate a string.
- Parameters
-
s pointer to a string n maximum length
- Returns
- pointer to a duplicated string (up to maximum length, always null-terminated), allocated with malloc or
nullptr
if allocation failed.
void * memcpy | ( | void *__restrict__ | dest, |
void const *__restrict__ | src, | ||
size_t | size | ||
) |
Copy a memory area.
- Parameters
-
dest destination buffer src source buffer size number of bytes to copy
- Returns
- pointer to destination
- Note
- The memory must not overlap!
void * memmove | ( | void * | dest, |
void const * | src, | ||
size_t | size | ||
) |
Copy a memory area while the source may overlap with the destination.
- Parameters
-
dest destination buffer src source buffer size number of bytes to copy
- Returns
- pointer to destination
void * memset | ( | void * | dest, |
int | pattern, | ||
size_t | size | ||
) |
Fill a memory area with a pattern.
- Parameters
-
dest destination buffer pattern single byte pattern size number of bytes to fill with pattern
- Returns
- pointer to destination
int memcmp | ( | const void * | s1, |
const void * | s2, | ||
size_t | n | ||
) |
Compare a memory area.
- Parameters
-
s1 first memory buffer s2 second memory buffer n number of bytes to compare
- Returns
- an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2.