Recall that strings in C are just arrays-of-char in which the
final character has numerical value 0. The standard library provides a
number of useful routines for manipulating strings.
strlen
strlen returns the number of characters in a string, not
including the zero-terminator. (Declared in string.h)
|
size_t strlen(const char* s); /* size_t is a predefined integer type */ |
strcmp
strcmp compares two strings lexicographically. It returns a
negative number if the first string is less than the second, zero if
the two strings are identical and a positive number if the first
string is greater than the second. (Declared in string.h.)
|
int strcmp(const char *s1, const char *s2); |
strcpy and strncpy
strcpy copies one string into another. The destination string
must have enough room for all the characters of the source string,
including room for the zero-terminator. The function returns a
pointer to the start of the destination string. If available you
should use strncpy instead which allows you to specify the amount of
space in the destination string. (Declared in
string.h.)
|
char *strcpy(char *destination_str, const char *source_str); /* size_t is a predefined integer type */ char *strncpy(char *destination_str, const char *source_str, size_t size); |
atof, atoi
atof and atoi convert a string to a floating-point number
and an integer respectively. (Declared in stdlib.h.)
|
double atof(const char *str); int atof(const char *str); |