-
Notifications
You must be signed in to change notification settings - Fork 2
/
swap_case.cpp
34 lines (28 loc) · 854 Bytes
/
swap_case.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
/*
Swap Case
#string manipulation
Have the function SwapCase(str) take the str parameter and swap the case of each
character. For example: if str is "Hello World" the output should be hELLO wORLD.
Let numbers and symbols stay the way they are.
Optimal: o(n), achieved: o(n)
*/
#include <iostream>
#include <string>
const size_t bigA = 65;
const size_t smalla = 97;
const size_t smallz = 122;
const size_t bigZ = 90;
const size_t shift = 32;
// affects only letters
std::string SwapCase(std::string str) {
for (auto i{0};i<str.length(); i++){
if ( str.at(i) <= bigZ && str.at(i) >= bigA) { str.at(i)+=shift; }
else if (str.at(i) <= smallz && str.at(i) >= smalla) { str.at(i)-=shift; }
}
return str;
}
int main(void) {
// keep this function call here
std::cout << SwapCase(coderbyteInternalStdinFunction(stdin));
return 0;
}