Designerdude13
Power Member
- Nov 22, 2010
- 648
- 195
Ok, So I'm trying to wrap the alphabet so it starts back at A when it's shifted by a value thats goes past the 26 letters. I tried putting the alphabet in array and wrapping, but I'm stuck. Please help!
Code:
char shiftChar(char c, int shift) {
if(!isalpha(c)) {
return c;
}
if(isalpha(c)) {
if(islower(c)) {
c = toupper(c);
}
c = ((c % 26) - 13);
int a[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
shift = (26 % shift);
int x = 0;
x = c + shift;
c = a[x];
return static_cast<char> (c);
}
}