-
Notifications
You must be signed in to change notification settings - Fork 2
/
letter_changes.cpp
40 lines (32 loc) · 1.04 KB
/
letter_changes.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
/*
Letter changes
#string manipulation
Have the function LetterChanges(str) take the str parameter being passed and modify
it using the following algorithm. Replace every letter in the string with the letter
following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every
vowel in this new string (a, e, i, o, u) and finally return this modified string.
Optimal: o(n), achieved: o(n)
*/
#include <iostream>
#include <string>
#include <set>
std::string LetterChanges(std::string str) {
// code goes here
std::set<char> vowSet = {'a', 'e', 'i', 'o', 'u'};
// assuming there are no uppercases in the beginning
for (int i{0}; i<str.length(); i++) {
if (isalpha(str.at(i))) {
str.at(i)=static_cast<char>(str.at(i)+1);
if (str.at(i) == '{') {str.at(i)='a';}
}
if (vowSet.find(str.at(i)) != vowSet.end()) {
str.at(i) = toupper(str.at(i));
}
}
return str;
}
int main(void) {
// keep this function call here
std::cout << LetterChanges(coderbyteInternalStdinFunction(stdin));
return 0;
}