Caesar Cipher

  • + 0 comments
    k=2
    s='middle-Outz'
    
    arr = list(s)
    finalarr=[]
    
    addingvar = k % 26
    
    for i in arr:
    #special characters
        if (ord(i) < 65 or (ord(i) > 90 and ord(i) < 97) or ord(i) > 122):
            finalarr.append(i)
            continue
    
        if(ord(i) >= 65 and ord(i) <= 90):
    
            if ((ord(i) + addingvar) > 122 or ((ord(i) + addingvar) > 90 and (ord(i) + addingvar) < 97) or (ord(i) + addingvar) > 90):
                temp = (ord(i)+addingvar) - 26
                finalarr.append(chr(temp))
                continue
                finalarr.append(chr(ord(i) + addingvar))
    
        if(ord(i) >= 97 and ord(i) <= 122):
    
            if ((ord(i) + addingvar) > 122 or ((ord(i) + addingvar) > 90 and (ord(i) + addingvar) < 97)):
                temp = (ord(i) + addingvar) - 26
                finalarr.append(chr(temp))
                continue
        finalarr.append(chr(ord(i)+addingvar))
    
    print("".join(finalarr))