-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path9.47.cpp
45 lines (40 loc) · 1.39 KB
/
9.47.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Exercise 9.47: Write a program that finds each numeric character and then
* each alphabetic character in the string "ab2c3d7R4E6". Write two versions of
* the program. The first should use find_first_of, and the second
* find_first_not_of.
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <string>
int main()
{
std::string s{"ab2c3d7R4E6"};
std::string numbers{"0123456789"};
std::cout << "string: " << s << '\n';
for (decltype(s.size()) pos = 0; (pos = s.find_first_of(numbers, pos))
!= std::string::npos; ++pos)
std::cout << "found number at index: " << pos
<< " element is " << s[pos] << '\n';
for (decltype(s.size()) pos = 0; (pos = s.find_first_not_of(numbers, pos))
!= std::string::npos; ++pos)
std::cout << "found letter at index: " << pos
<< " element is " << s[pos] << '\n';
std::cout << '\n';
std::string alphabets;
alphabets.reserve(60);
for (char c = 'a'; c <= 'z'; ++c) {
alphabets.push_back(c);
alphabets.push_back(toupper(c));
}
for (decltype(s.size()) pos = 0; (pos = s.find_first_not_of(alphabets, pos))
!= std::string::npos; ++pos)
std::cout << "found letter at index: " << pos
<< " element is " << s[pos] << '\n';
for (decltype(s.size()) pos = 0; (pos = s.find_first_of(alphabets, pos))
!= std::string::npos; ++pos)
std::cout << "found letter at index: " << pos
<< " element is " << s[pos] << '\n';
return 0;
}