String Handling
There is no
basic type for strings in C. A string is simply a sequence of
characters ending with the string terminator, stored in a
char
array. A string is represented by a
char
pointer that points to the first character in
the string.
The customary functions for manipulating strings are declared in string.h . Those functions that modify a string return a pointer to the modified string. The functions used to search for a character or a substring return a pointer to the occurrence found, or a null pointer if the search was unsuccessful.
-
char *
strcat
( char *s1
, const char *s2
); Appends the string
s2
to the end ofs1
. The first character copied froms2
replaces the string terminator character ofs1
.-
char *
strchr
( const char *s
, intc
); Locates the first occurrence of the character
c
in the strings
.-
int
strcmp
( const char *s1
, const char *s2
); Compares the strings
s1
ands2
, and returns a value that is greater than, equal to, or less than0
to indicate whethers1
is greater than, equal to, or less thans2
. A string is greater than another if the first character code in it which differs from the corresponding character code in the other string is greater than that character code.-
int
strcoll
( const char *s1
, const char *s2
); Transforms an internal copy of the strings
s1
ands2
using the functionstrxfrm()
, then compares them usingstrcmp()
and returns the result.-
char *
strcpy
( char *s1
, const char *s2
); Copies
s2
to thechar
array referenced bys1
. This ...
Get C Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.