-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathstrings.cpp
86 lines (71 loc) · 3.5 KB
/
strings.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//*****************************************************************************************************
// String Processing
//
// This program takes a string and uses various string functions to extract and display
// information about the string such as the length, number of letters, digits, upper and lower
// case letters, specific characters and indexes of certain characters.
//
//*****************************************************************************************************
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
//*****************************************************************************************************
void sentenceData(const string sentence);
//*****************************************************************************************************
int main() {
string sentence("This is a 101 SAMPLE to demonstrate string processing");
cout << "------------------------------------------------------\n"
<< sentence
<< "\n------------------------------------------------------" << endl;
sentenceData(sentence);
return 0;
}
//*****************************************************************************************************
void sentenceData(const string sentence) {
int length,
alphaCount = 0,
digitCount = 0,
lowerCount = 0,
upperCount = 0,
first_s,
second_s;
length = sentence.length(); // get the length of the string
first_s = sentence.find('s'); // find the first 's' in the string
second_s = sentence.find('s', first_s + 1); // find the second 's' in the string
for (int i = 0; i < length; ++i) { // bool return types: isalpha(), isdigit(), islower(), isupper()
if (isalpha(sentence[i]))
alphaCount++;
if (isdigit(sentence[i]))
digitCount++;
if (islower(sentence[i]))
lowerCount++;
if (isupper(sentence[i]))
upperCount++;
}
cout << "The size of the string: " << length
<< "\nThe number of letters in the string: " << alphaCount
<< "\nThe number of digits in the string: " << digitCount
<< "\nThe number of lower case letters in the string: " << lowerCount
<< "\nThe number of upper case letters in the string: " << upperCount
<< "\nThe character that is at index 12 of the string: " << sentence.at(12) // at() is the same as []
<< "\nThe first character of the string: " << sentence.front() // front() is the same as [0]
<< "\nThe last character of the string: " << sentence.back() // back() is the same as [length - 1]
<< "\nThe index of the first 's' in the string: " << first_s
<< "\nThe index of the second 's' in the string: " << second_s << endl;
}
//*****************************************************************************************************
/*
------------------------------------------------------
This is a 101 SAMPLE to demonstrate string processing
------------------------------------------------------
The size of the string: 53
The number of letters in the string: 42
The number of digits in the string: 3
The number of lower case letters in the string: 35
The number of upper case letters in the string: 7
The character that is at index 12 of the string: 1
The last character of the string: g
The index of the first 's' in the string: 3
The index of the second 's' in the string: 6
*/