#include <stdio.h>
/*************** CAESAR CYPHER *****************
-Take a charater array input
-Loop through 26 character shifts
-Print all possibilities
************************************************/
void Brute_Force(char *message)// Character array pointer
{
char *i;// Copy message for iteration: Stop at '/0'
int j;// Iterate through the alphabet
char t;//Temp variable for current letter
for(j = 0; j < 26; j++)
{
printf("\n----------Shift %i----------\n", j);//Aesthetics
for(i = message; *i != '\0'; i++)
{
//Multiple of 26 to start at original message
unsigned char t = ((*i + j)%26) + 104;
// Only loop 97 to 122. ASCII 'a'-'z'
if(t > 122){t = t - 26;}
printf("%c", t);
}
}
}
int main()
{
char mess[26] = "ynkooejcpdanqxeykjrbdofgkq";
Brute_Force(mess);
return 0;
}
Before changing t
to an unsigned char
instead of a regular char
, every time 'f'(102) or 'g'(103) would come up, it would output t = -127/-128
instead of 127/128
. I'm just wondering what would have caused it to change to negative. It was only for 'f' and 'g', every other letter was correct.
a sample output would be:
ynkooejcpdanqxeykjrbdo��kq