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. 20.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
.
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. 20.35 uses strchr
(lines 14 and 22) to search for the first occurrences of 'a'
and 'z'
in the string "This is a test"
.
1 // Fig. 20.35: fig20_35.cpp
2 // Using strchr.
3 #include <iostream>
4 #include <cstring> // strchr prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "This is a test";
10 char character1 = 'a';
11 char character2 = 'z';
12
13 // search for character1 in string1
14 if ( strchr( string1, character1 ) != NULL
)
15 cout << ''' << character1 << "' was found in ""
16 << string1 << "".
";
17 else
18 cout << ''' << character1 << "' was not found in ""
19 << string1 << "".
";
20
21 // search for character2 in string1
22 if ( strchr( string1, character2 ) != NULL
)
23 cout << ''' << character2 << "' was found in ""
24 << string1 << "".
";
25 else
26 cout << ''' << character2 << "' was not found in ""
27 << string1 << ""." << endl;
28 } // end main
'a' was found in "This is a test".
'z' was not found in "This is a test".
Function strcspn (Fig. 20.36, line 15) 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.
1 // Fig. 20.36: fig20_36.cpp
2 // Using strcspn.
3 #include <iostream>
4 #include <cstring> // strcspn prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "The value is 3.14159";
10 const char *string2 = "1234567890";
11
12 cout << "string1 = " << string1 << "
string2 = " << string2
13 << "
The length of the initial segment of string1"
14 << "
containing no characters from string2 = "
15 << strcspn( string1, string2 ) << endl;
16 } // end main
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 13 of Fig. 20.37 locates the first occurrence in string1
of any character from string2
.
1 // Fig. 20.37: fig20_37.cpp
2 // Using strpbrk.
3 #include <iostream>
4 #include <cstring> // strpbrk prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "This is a test";
10 const char *string2 = "beware";
11
12 cout << "Of the characters in "" << string2 << ""
'"
13 << *strpbrk( string1, string2 ) << "' is the first character "
14 << "to appear in
"" << string1 << '"' << endl;
15 } // end main
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 15 of Fig. 20.38 searches for the last occurrence of the character 'z'
in the string "A zoo has many animals including zebras"
.
1 // Fig. 20.38: fig20_38.cpp
2 // Using strrchr.
3 #include <iostream>
4 #include <cstring> // strrchr prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "A zoo has many animals including zebras";
10 char c = 'z';
11
12 cout << "string1 = " << string1 << "
" << endl;
13 cout << "The remainder of string1 beginning with the
"
14 << "last occurrence of character '"
15 << c << "' is: "" << strrchr( string1, c ) << '"' << endl;
16 } // end main
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. 20.39, line 15) 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.
1 // Fig. 20.39: fig20_39.cpp
2 // Using strspn.
3 #include <iostream>
4 #include <cstring> // strspn prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "The value is 3.14159";
10 const char *string2 = "aehils Tuv";
11
12 cout << "string1 = " << string1 << "
string2 = " << string2
13 << "
The length of the initial segment of string1
"
14 << "containing only characters from string2 = "
15 << strspn( string1, string2 ) << endl;
16 } // end main
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 15 of Fig. 20.40 uses strstr
to find the string "def"
in the string "abcdefabcdef"
.
1 // Fig. 20.40: fig20_40.cpp
2 // Using strstr.
3 #include <iostream>
4 #include <cstring> // strstr prototype
5 using namespace std;
6
7 int main()
8 {
9 const char *string1 = "abcdefabcdef";
10 const char *string2 = "def";
11
12 cout << "string1 = " << string1 << "
string2 = " << string2
13 << "
The remainder of string1 beginning with the
"
14 << "first occurrence of string2 is: "
15 << strstr( string1, string2 ) << endl;
16 } // end main
string1 = abcdefabcdef
string2 = def
The remainder of string1 beginning with the
first occurrence of string2 is: defabcdef