-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_case_using_n.cpp
38 lines (28 loc) · 1.13 KB
/
swap_case_using_n.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
//Your job is to change the given string s using a non-negative integer n.
//Each bit in n will specify whether or not to swap the case for each alphabetic character in s: if the bit is 1, swap the case; if its 0, leave it as is. When you finish with the last bit of n, start again with the first bit.
//You should skip the checking of bits when a non-alphabetic character is encountered, but they should be preserved in their original positions.
//Examples
//swap("Hello world!", 11) --> "heLLO wORLd!"
//...because 11 is 1011 in binary, so the 1st, 3rd, 4th, 5th, 7th, 8th and 9th alphabetical characters have to be swapped:
//H e l l o w o r l d !
//1 0 1 1 1 x 0 1 1 1 0 x
//^ ^ ^ ^ ^ ^ ^
#include <string>
std::string swap(std::string s, int n) {
std::vector<int> bits;
while (n) {
bits.insert(bits.begin(), n & 1);
n >>= 1;
}
if (!bits.size())
return s;
std::string result = "";
int i = 0;
for (char c : s) {
if (isalpha(c) && bits[i++ % bits.size()]) {
c = c < 97 ? tolower(c) : toupper(c);
}
result += c;
}
return result;
}