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".

Fig. 22.35 String-search function strchr.

Alternate View

 1  // Fig. 22.35: fig22_35.cpp
 2  // Using strchr.
 3  #include <iostream>
 4  #include <cstring> // strchr prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"This is a test"};
 9     char character1{'a'};
10     char character2{'z'};
11
12     // search for character1 in string1
13     if (strchr(string1, character1) != NULL) {
14        cout << ''' << character1 << "' was found in ""
15            << string1 << "".
";
16     }
17     else {
18        cout << ''' << character1 << "' was not found in ""
19           << string1 << "".
";
20     }
21
22     // search for character2 in string1
23     if (strchr(string1, character2) != NULL) {
24        cout << ''' << character2 << "' was found in ""
25           << string1 << "".
";
26     }
27     else {
28        cout << ''' << character2 << "' was not found in ""
29           << string1 << ""." << endl;
30     }
31  }

'a' was found in "This is a test".
'z' was not found in "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.

Fig. 22.36 String-search function strcspn.

Alternate View

 1  // Fig. 22.36: fig22_36.cpp
 2  // Using strcspn.
 3  #include <iostream>
 4  #include <cstring> // strcspn prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"The value is 3.14159"};
 9     const char* string2{"1234567890"};
10
11     cout << "string1 = " << string1 << "
string2 = " << string2
12        << "

The length of the initial segment of string1"
13        << "
containing no characters from string2 = "
14        << strcspn(string1, string2) << endl;
15  }

string1 = The value is 3.14159
string2 = 1234567890

The length of the initial segment of string1
containing no characters from string2 = 13

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.

Fig. 22.37 String-search function strpbrk.

Alternate View

 1  // Fig. 22.37: fig22_37.cpp
 2  // Using strpbrk.
 3  #include <iostream>
 4  #include <cstring> // strpbrk prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"This is a test"};
 9     const char* string2{"beware"};
10
11     cout << "Of the characters in "" << string2 << ""
'"
12        << *strpbrk(string1, string2) << "' is the first character "
13        << "to appear in
"" << string1 << '"' << endl;
14  }

Of the characters in "beware"
'a' is the first character to appear in
"This is a test"

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".

Fig. 22.38 String-search function strrchr.

Alternate View

 1  // Fig. 22.38: fig22_38.cpp
 2  // Using strrchr.
 3  #include <iostream>
 4  #include <cstring> // strrchr prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"A zoo has many animals including zebras"};
 9     char c{'z'};
10
11     cout << "string1 = " << string1 << "
" << endl;
12     cout << "The remainder of string1 beginning with the
"
13        << "last occurrence of character '"
14        << c << "' is: "" << strrchr(string1, c) << '"' << endl;
15  }

string1 = A zoo has many animals including zebras
The remainder of string1 beginning with the
last occurrence of character 'z' is: "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.

Fig. 22.39 String-search function strspn.

Alternate View

 1  // Fig. 22.39: fig22_39.cpp
 2  // Using strspn.
 3  #include <iostream>
 4  #include <cstring> // strspn prototype
 5  using namespace std;
 6
 7  int main() {
 8     const char* string1{"The value is 3.14159"};
 9     const char* string2{"aehils Tuv"};
10
11     cout << "string1 = " << string1 << "
string2 = " << string2
12        << "

The length of the initial segment of string1
"
13        << "containing only characters from string2 = "
14        << strspn(string1, string2) << endl;
15  }

string1 = The value is 3.14159
string2 = aehils Tuv

The length of the initial segment of string1
containing only characters from string2 = 13

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".

Fig. 22.40 String-search function strstr.

Alternate View

1  // Fig. 22.40: fig22_40.cpp
2  // Using strstr.
3  #include <iostream>
4  #include <cstring> // strstr prototype
5  using namespace std;
6
7  int main() {
8     const char* string1{"abcdefabcdef"};
9     const char* string2{"def"};
10
11    cout << "string1 = " << string1 << "
string2 = " << string2
12       << "

The remainder of string1 beginning with the
"
13       << "first occurrence of string2 is: "
14       << strstr(string1, string2) << endl;
15  }

string1 = abcdefabcdef
string2 = def

The remainder of string1 beginning with the
first occurrence of string2 is: defabcdef
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset