Take the 2-minute tour ×

I was here earlier and got a hand adding to chars ascii references to increment the cipher shift on each letter. However i have no idea how to fix the problem of characters being higher than 'z'.

Can someone give me a hint towards how to wrap around when the characters reach the end of the alphabet. I don't expect anyone to do my work for me, of course.

char decrypt(char letter)
int increment = 9;
if(letter == ' ')
    return letter;
letter += increment;
return letter;

int main()
char message[446]; int i = 0; char space = ' ';
ifstream in("encryptedText.txt");
if(in.getline(message, 446))
            cout << decrypt(tolower(message[i])) << endl;
{cout << "Can't read file" << endl;}
cout << endl;  
share|improve this question
@Marobri... it's worth bearing in mind that the ascii characters can be directly translated into digits. It would help you if you thought about them as digits... google search for ascii table and you can see the universally applicable numeric values that you can use for future "letter maths". –  Dennis Dec 13 '11 at 14:21
add comment

1 Answer

up vote 4 down vote accepted

The modulo operation is your friend. Whenever integers form a ring instead of a sequence, a modulo operation can be applied, like:

5 % 4 == 17 % 4

You need to compute this in the integer space relative to 'a', of course, subtracting 'a' appropriately.

share|improve this answer
That's a good answer to a homework question. It helps without doing the asker's work. +1 from me. –  sbi Dec 13 '11 at 12:53
Thanks @thiton, appreciate it! –  Marobri Dec 13 '11 at 13:07
add comment

Your Answer


