-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathalliteration.cpp
65 lines (51 loc) · 1.52 KB
/
alliteration.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
// https://www.urionlinejudge.com.br/judge/en/problems/view/1263
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void split_by_space(vector<string> &words, string phrase) {
int index_start=0, length;
string aux;
for (int i = 0; i < phrase.size(); i++) {
if (phrase[i] == ' ') {
length = i - index_start;
aux = phrase.substr(index_start, length);
words.push_back(aux);
index_start = i + 1;
} else if (i == phrase.size()-1) {
length = i - index_start + 1;
aux = phrase.substr(index_start, length);
words.push_back(aux);
}
}
}
int main() {
string phrase;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVXWYZabcdefghijklmnopqrstuvxwyzABCDEFGHIJKLMNOPQRSTUVXWYZ";
vector<string> words;
while (getline(cin, phrase)) {
vector<int> alli;
alli.push_back(0);
split_by_space(words, phrase);
char current_letter = words[0][0]; // first letter of the first word
if (words.size() > 1) {
for (int i = 1; i < words.size(); i++) {
int letter_pos = alphabet.find(words[i][0]);
if (current_letter == alphabet[letter_pos] || current_letter == alphabet[letter_pos + 26]) {
alli.back()++;
} else {
alli.push_back(0);
}
current_letter = words[i][0];
}
int counter = 0;
for (int i = 0; i < alli.size(); i++) if (alli[i] > 0) counter++;
cout << counter << endl;
} else {
cout << 0 << endl;
}
words.clear();
alli.clear();
}
return 0;
}