
please wait
|
|
Hi, I'm just starting to teach myself C++ and using Codewars to gain some experience. |
|
|
On line 12 I copy s[a] into str[c], but why is this an issue? |
Because at the beginning str has no content so it's length is 0 - but this is attempting to access the non-existent str[c] char. Even if c is 0, at the start str[0] is not defined as str is empty. |
… page fault is usually a reference to an operating system and hardware marriage where memory is moved in blocks (pages) in and out of the CPU cache… |
I can't quite grasp how you are using the &~ and | bitwise operators to ensure your line 9 is always upppercase, and your line 10 is repeating itimes lowercase. |
'A' | 0x20 == 'a'; // 0100 0001 | 0010 0000 == 0x0110 0001 'a' | 0x20 == 'a'; // 0110 0001 | 0010 0000 == 0x0110 0001 'A' &~ 0x20 == 'A'; // 0100 0001 & ~0010 0000 == 0100 0001 & 1101 1111 == 0x0100 0001 'a' &~ 0x20 == 'A'; // 0110 0001 & ~0010 0000 == 0110 0001 & 1101 1111 == 0x0100 0001 |
Both our approaches assume an ASCII-compatible text encoding. In ASCII-compatible encodings the difference between an uppercase letter 'A'-'Z' and a lowercase letter 'a'-'z' is that the sixth bit is unset in the former and set in the latter. For example 'A' is represented by a byte containing the binary value 1000001 while 'a' is represented by a byte with value 1100001. |
The bit math is intended to set and unset the sixth bit. 'A' | 0x20 == 'a'; // 0100 0001 | 0010 0000 == 0x0110 0001 'a' | 0x20 == 'a'; // 0110 0001 | 0010 0000 == 0x0110 0001 'A' &~ 0x20 == 'A'; // 0100 0001 & ~0010 0000 == 0100 0001 & 1101 1111 == 0x0100 0001 'a' &~ 0x20 == 'A'; // 0110 0001 & ~0010 0000 == 0110 0001 & 1101 1111 == 0x0100 0001 |
Line 22 should be return str; What do you suppose str.c_str() does and why do you think it was needed? |