Search Functions of the Pointer-Based 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.28. Note that 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.
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. |
Portability Tip 22.6
Type size_t is a system-dependent synonym for either type unsigned long or type unsigned int. |
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.29 uses strchr (lines 17 and 25) to search for the first occurrences of 'a' and 'z' in the string "This is a test".
Figure 22.29. String-search function strchr.
(This item is displayed on pages 1090 - 1091 in the print version)
1 // Fig. 22.29: fig22_29.cpp 2 // Using strchr. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strchr prototype 8 using std::strchr; 9 10 int main() 11 { 12 const char *string1 = "This is a test"; 13 char character1 = 'a'; 14 char character2 = 'z'; 15 16 // search for character1 in string1 17 if ( strchr( string1, character1 ) != NULL ) 18 cout << ''' << character1 << "' was found in "" 19 << string1 << "". "; 20 else 21 cout << ''' << character1 << "' was not found in "" 22 << string1 << "". "; 23 24 // search for character2 in string1 25 if ( strchr( string1, character2 ) != NULL ) 26 cout << ''' << character2 << "' was found in "" 27 << string1 << "". "; 28 else 29 cout << ''' << character2 << "' was not found in "" 30 << string1 << ""." << endl; 31 32 return 0; 33 } // end main
|
Function strcspn (Fig. 22.30, line 18) 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.
Figure 22.30. String-search function strcspn.
1 // Fig. 22.30: fig22_30.cpp 2 // Using strcspn. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strcspn prototype 8 using std::strcspn; 9 10 int main() 11 { 12 const char *string1 = "The value is 3.14159"; 13 const char *string2 = "1234567890"; 14 15 cout << "string1 = " << string1 << " string2 = " << string2 16 << " The length of the initial segment of string1" 17 << " containing no characters from string2 = " 18 << strcspn( string1, string2 ) << endl; 19 return 0; 20 } // end main
|
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 16 of Fig. 22.31 locates the first occurrence in string1 of any character from string2.
Figure 22.31. String-search function strpbrk.
1 // Fig. 22.31: fig22_31.cpp 2 // Using strpbrk. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strpbrk prototype 8 using std::strpbrk; 9 10 int main() 11 { 12 const char *string1 = "This is a test"; 13 const char *string2 = "beware"; 14 15 cout << "Of the characters in "" << string2 << "" '" 16 << *strpbrk( string1, string2 ) << "' is the first character " 17 << "to appear in "" << string1 << '"' << endl; 18 return 0; 19 } // end main
|
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 18 of Fig. 22.32 searches for the last occurrence of the character 'z' in the string "A zoo has many animals including zebras".
Figure 22.32. String-search function strrchr.
(This item is displayed on pages 1092 - 1093 in the print version)
1 // Fig. 22.32: fig22_32.cpp 2 // Using strrchr. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strrchr prototype 8 using std::strrchr; 9 10 int main() 11 { 12 const char *string1 = "A zoo has many animals including zebras"; 13 char c = 'z'; 14 15 cout << "string1 = " << string1 << " " << endl; 16 cout << "The remainder of string1 beginning with the " 17 << "last occurrence of character '" 18 << c << "' is: "" << strrchr( string1, c ) << '"' << endl; 19 return 0; 20 } // end main
|
Function strspn (Fig. 22.33, line 18) 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.
Figure 22.33. String-search function strspn.
(This item is displayed on page 1093 in the print version)
1 // Fig. 22.33: fig22_33.cpp 2 // Using strspn. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strspn prototype 8 using std::strspn; 9 10 int main() 11 { 12 const char *string1 = "The value is 3.14159"; 13 const char *string2 = "aehils Tuv"; 14 15 cout << "string1 = " << string1 << " string2 = " << string2 16 << " The length of the initial segment of string1 " 17 << "containing only characters from string2 = " 18 << strspn( string1, string2 ) << endl; 19 return 0; 20 } // end main
|
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 18 of Fig. 22.34 uses strstr to find the string "def" in the string "abcdefabcdef".
Figure 22.34. String-search function strstr.
(This item is displayed on pages 1093 - 1094 in the print version)
1 // Fig. 22.34: fig22_34.cpp 2 // Using strstr. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include // strstr prototype 8 using std::strstr; 9 10 int main() 11 { 12 const char *string1 = "abcdefabcdef"; 13 const char *string2 = "def"; 14 15 cout << "string1 = " << string1 << " string2 = " << string2 16 << " The remainder of string1 beginning with the " 17 << "first occurrence of string2 is: " 18 << strstr( string1, string2 ) << endl; 19 return 0; 20 } // end main
|