I'm practicing on Code Wars and I've encountered a strange problem. Omitting some details: the task is to replace each digit by its complement to 9, and shift each letter by a given number. To do so I've made this loop, which I believe should change each character based on its ANSI code representation:
char *str = "ABC 123!"; // example
int n = 1; // example
for (char *c = str; *c; c++) {
printf("Before: *c = %d\n", *c);
if (*c > 47 && *c < 58) {
*c = 105 - *c;
// replace digit
}
if (*c > 64 && *c < 91) {
*c = (*c - 64 + number) % 26 + 64;
// replace letter
}
printf("After: *c = %d\n", *c);
}
I've made various log statements in various places from which I can tell that some condition checking fails and I don't enter the if
block, while the other succeeds and I do enter the if
block, but as soon as I run into the *c = ...;
statement, the test crashes (also tried running the same code in a separate IDE, but the execution still suddenly stops).
Why would this happen?