#caeser encryption special case?!

23 messages · Page 1 of 1 (latest)

ivory osprey
#

can someone explain why my caesar solving algorithm works for every letter except of 'Z' cause 'Z' should turn to 'O' but it turns to 'arrow down'

#include<iostream>
#include<string> 

void unsolve(std::string nachricht);
int offset = 0;
int main()
{
    std::string nachricht 
    unsolve(nachricht);
    return 0;

}
void unsolve(std::string nachricht){
int highest = 0;
int index = 0;
int letters[24] = {};
for (int i = 0; i < nachricht.size() ; i++)
{
    if (nachricht[i] >= 'A' && nachricht[i] <= 'Z')
    {
    nachricht[i] = nachricht[i] - 'A';
    letters[nachricht[i]]++;
    }
}
for (int i = 0; i < sizeof(letters)/sizeof(letters[1]); i++)
{
    if(letters[i] > highest){
        highest = letters[i];
        index = i;}
}
for (int i = 0; i < nachricht.size(); i++)
{
    if (nachricht[i] >= 0 && nachricht[i] <= 24)
    {
        nachricht[i] = nachricht[i] + 'A';
    }
    if(nachricht[i] >= 76 && nachricht[i] <= 90) {
    nachricht[i] = nachricht[i] - (index - 4);
    }
    else if(nachricht[i] >= 65 && nachricht[i] <= 75)
    {
        nachricht[i] = nachricht[i] + 15;
    }
}
}```
forest kestrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ivory osprey
#
std::string nachricht =    "OTP NLPDLC - GPCDNSWFPDDPWFYR(LFNS LWD NLPDLC - NSTQQCP, "
                        "NLPDLC - LWRZCTESXFD, NLPDLC - GPCDNSTPMFYR, GPCDNSTPMPNSTQQCP "
                        "ZOPC LWD PTYQLNSPC NLPDLC MPKPTNSYPE) TDE PTY PTYQLNSPD DJXXPECTDNSPD "
                        "GPCDNSWFPDDPWFYRDGPCQLSCPY, OLD LFQ OPC XZYZRCLASTDNSPY FYO "
                        "XZYZLWASLMPETDNSPY DFMDETEFETZY MLDTPCE.LWD PTYPD OPC PTYQLNSDEPY "
                        "FYO FYDTNSPCDEPY GPCQLSCPY OTPYE PD SPFEP SLFAEDLPNSWTNS OLKF, "
                        "RCFYOACTYKTATPY OPC VCJAEZWZRTP LYDNSLFWTNS OLCKFDEPWWPY.OPC "
                        "PTYQLNSSPTE SLWMPC HPCOPY ZQEXLWD YFC OTP 26 MFNSDELMPY OPD "
                        "WLEPTYTDNSPY LWASLMPED ZSYP FYEPCDNSPTOFYR GZY RCZDD - "
                        "FYO VWPTYMFNSDELMPY LWD LWASLMPE QFPC VWLCEPIE FYO RPSPTXEPIE GPCHPYOPE "
                        "FYO DZYOPCKPTNSPY, DLEKKPTNSPY FDH. YTNSE MPLNSEPE.";```
#

and why does it work when I do cpp nachricht[i] = nachricht[i] - (index - 4); and not cpp nachricht[i] = nachricht[i] - index;

rugged sierra
ivory osprey
#

yes the most common letter is E

rugged sierra
#

and since E is the 5th letter (index 4), that's what you want to offset by

ivory osprey
#

ah okay that makes sense

#

but why is Z not after this calculation O but arrowdown

#

because its going into this if statement cpp if(nachricht[i] >= 76 && nachricht[i] <= 90) { nachricht[i] = nachricht[i] - (index - 4); }

#

but idk why its not just doing -11 then

rugged sierra
#

index is 15 right?

ivory osprey
#

yes

#

its O

rugged sierra
#

idk, use you debugger

ivory osprey
#

is that then a coincidence?

rugged sierra
#

or maybe show a screenshot of the arrowdown

ivory osprey
#

I got it

#
 if (nachricht[i] >= 0 && nachricht[i] <= 24)
    {
        nachricht[i] = nachricht[i] + 'A';
    }``` this needs to be <= 25 not <=24
#

but thank you

#

for ur help

#

!solved