0
#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
  • 3
    `char` is a signed type on many systems, so when it exceeds 127, you have overflow and hence undefined behavior. Commonly this results in wrapping around to negative values in the usual way of two's complement arithmetic. Applying the `%` operator to a negative value gives a negative result. – Nate Eldredge Feb 24 '21 at 02:14
  • As a `char`, the expression `((*i + j)%26) + 104` would be negative if it exceeds 127. I suspect this expression did that when the character was `f` or `g`. In fact, it happens any time the expression `(*i + h)%26` exceeds 23. Incidentally, the first `char t` serves no purpose in the code version you show currently. – lurker Feb 24 '21 at 02:16
  • Why are you adding 104? – smac89 Feb 24 '21 at 02:17
  • 1
    Don't use the magic number `122`. If you want to compare to `z`, then do it directly: `if( t > 'z') ...` – William Pursell Feb 24 '21 at 02:47
  • as said, the first `char t` is unused. And you should move `i` and `j` declaration to inside the `for` loop. Variables should be declared in the smallest scope as possible – phuclv Feb 24 '21 at 03:16
  • 1
    @smac89 It's a multiple of 26. I did it so the output started on the original message. I've learned that the better way to do it is `((*i - 97 + j)%26) + 97` – PhyllaciousArmadillo Feb 24 '21 at 20:51
  • @lurker Thanks, I I forgot to take that out... – PhyllaciousArmadillo Feb 24 '21 at 20:53
  • @phuclv Is there a specific reason for this, or is it just a general rule? – PhyllaciousArmadillo Feb 24 '21 at 20:54
  • @PhyllaciousArmadillo it's general rule in almost every language [Declaring variables inside loops, good practice or bad practice?](https://stackoverflow.com/q/7959573/995714), [Declare variables at top of function or in separate scopes?](https://stackoverflow.com/q/3773396/995714), [Minimalizing variable's scope in C++](https://stackoverflow.com/q/59578451/995714), [“Variables should be given the smallest scope”. Really?](https://stackoverflow.com/q/42847980/995714), [Should object be created with minimal scope?](https://stackoverflow.com/q/7485877/995714) – phuclv Feb 25 '21 at 01:16
  • [Optimal declaration of variables with regards to scope](https://stackoverflow.com/q/4075246/995714). And it appears in pretty much every C or C++ coding standard: https://wiki.sei.cmu.edu/confluence/display/c/DCL19-C.+Minimize+the+scope+of+variables+and+functions, https://www.appentra.com/knowledge/checks/pwr002/ – phuclv Feb 25 '21 at 01:17

0 Answers0