22.10 Search Functions of the C String-Handling Library
This section presents the functions of the string-handling library used to search strings for characters and other strings. The functions are summarized in Fig. 22.34. Functions strcspn and strspn specify return type size_t. Type size_t is a type defined by the standard as the integral type of the value returned by operator sizeof.
Fig. 22.34 Search functions of the C string-handling library.
Prototype
Description
char* strchr(const char* s, int c)
Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a null pointer is returned.
char* strrchr(const char* s, int c)
Searches from the end of string s and locates the last occurrence of character c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a null pointer is returned.
size_t strspn(const char* s1, const char* s2)
Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.
char* strpbrk(const char* s1, const char* s2)
Locates the first occurrence in string s1 of any character in string s2. If a character from string s2 is found, a pointer to the character in string s1 is returned. Otherwise, a null pointer is returned.
size_t strcspn(const char* s1, const char* s2)
Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.
char* strstr(const char* s1, const char* s2)
Locates the first occurrence in string s1 of string s2. If the string is found, a pointer to the string in s1 is returned. Otherwise, a null pointer is returned.
Function strchr searches for the first occurrence of a character in a string. If the character is found, strchr returns a pointer to the character in the string; otherwise, strchr returns a null pointer. The program of Fig. 22.35 uses strchr (lines 13 and 23) to search for the first occurrences of 'a' and 'z' in the string "This is a test".
Function strcspn (Fig. 22.36, line 14) determines the length of the initial part of the string in its first argument that does not contain any characters from the string in its second argument. The function returns the length of the segment.
Function strpbrk searches for the first occurrence in its first string argument of any character in its second string argument. If a character from the second argument is found, strpbrk returns a pointer to the character in the first argument; otherwise, strpbrk returns a null pointer. Line 12 of Fig. 22.37 locates the first occurrence in string1 of any character from string2.
Function strrchr searches for the last occurrence of the specified character in a string. If the character is found, strrchr returns a pointer to the character in the string; otherwise, strrchr returns 0. Line 14 of Fig. 22.38 searches for the last occurrence of the character 'z' in the string "A zoo has many animals including zebras".
Function strspn (Fig. 22.39, line 14) determines the length of the initial part of the string in its first argument that contains only characters from the string in its second argument. The function returns the length of the segment.
Function strstr searches for the first occurrence of its second string argument in its first string argument. If the second string is found in the first string, a pointer to the location of the string in the first argument is returned; otherwise, it returns 0. Line 14 of Fig. 22.40 uses strstr to find the string "def" in the string "abcdefabcdef".