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 of s1. The first character copied from s2 replaces the string terminator character of s1.

char *strchr( const char *s, int c );

Locates the first occurrence of the character c in the string s.

int strcmp( const char *s1, const char *s2 );

Compares the strings s1 and s2, and returns a value that is greater than, equal to, or less than 0 to indicate whether s1 is greater than, equal to, or less than s2. 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 and s2 using the function strxfrm(), then compares them using strcmp() and returns the result.

char *strcpy( char *s1, const char *s2 );

Copies s2 to the char array referenced by s1. 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.