user5670271
user5670271

Reputation: 3

How can I make my Vigenère Cipher ignore spaces in the original message

Im trying to make a Vigenère Cipher but I can't seem to find a way to implement a feature that ignores in-putted white spaces when entering the message and then printing the final for example: I enter the starting message: "python computing" then I enter the key as: "stack" I expect to get if the program ignores spaces in the original message: "isukzg wppannjqr" but instead I get: "isukzgwppannjqr". Anyone know how I can solve this. I have considered using ords but I havent found a way to implement it. Code below:

def translateMessage(key, message, mode):
    translated = ""

    keyIndex = 0
    key = key.upper()

    for symbol in message:
        xyz = alphabet.find(symbol.upper())
        if xyz != -1:
            if mode == 'encrypt' or 'e':
                xyz += alphabet.find(key[keyIndex]) + 1
            elif mode == 'decrypt' or 'd':
                xyz -= alphabet.find(key[keyIndex]) + 1

            xyz %= len(alphabet)

            if symbol.isupper():
                translated += alphabet[xyz]
            elif symbol.islower():
                translated += alphabet[xyz].lower()

            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0

    return translated

if __name__ == '__main__':
    fetch_user_inputs()

Upvotes: 0

Views: 1190

Answers (2)

Middle Schooler
Middle Schooler

Reputation: 3

You can do better than that, this code can even return the format along with the spaces, Eg. Capital Letters, Lowercase Letters!

def shift_dict(Caesar, Shift):
  dic_len = len(Caesar)
  Shift = Shift % dic_len
  list_dic = [(k,v) for k, v in iter(Caesar.items())]
  Shifted = {
    list_dic[x][0]: list_dic[(x - Shift) % dic_len][1]
    for x in range(dic_len)
  }
  return Shifted

Viginere = {
  "A":0,
  "B":1,
  "C":2,
  "D":3,
  "E":4,
  "F":5,
  "G":6,
  "H":7,
  "I":8,
  "J":9,
  "K":10,
  "L":11,
  "M":12,
  "N":13,
  "O":14,
  "P":15,
  "Q":16,
  "R":17,
  "S":18,
  "T":19,
  "U":20,
  "V":21,
  "W":22,
  "X":23,
  "Y":24,
  "Z":25
}
VFU = {
  0:"A",
  1:"B",
  2:"C",
  3:"D",
  4:"E",
  5:"F",
  6:"G",
  7:"H",
  8:"I",
  9:"J",
  10:"K",
  11:"L",
  12:"M",
  13:"N",
  14:"O",
  15:"P",
  16:"Q",
  17:"R",
  18:"S",
  19:"T",
  20:"U",
  21:"V",
  22:"W",
  23:"X",
  24:"Y",
  25:"Z"
}
VFL = {
  0:"a",
  1:"b",
  2:"c",
  3:"d",
  4:"e",
  5:"f",
  6:"g",
  7:"h",
  8:"i",
  9:"j",
  10:"k",
  11:"l",
  12:"m",
  13:"n",
  14:"o",
  15:"p",
  16:"q",
  17:"r",
  18:"s",
  19:"t",
  20:"u",
  21:"v",
  22:"w",
  23:"x",
  24:"y",
  25:"z"
}

Asker = int(input("Do you want to... 1. Encode, or 2. Decode? "))
X = 0
Lister = []
Text = list(str(input("")))
Key = list(str(input("")).upper())
Z = 0

if Asker == 1:
  for i in range(len(Text)):
    Shift = Viginere[Key[(Z % len(Key))]]
    if Text[X].isalpha():
      LetterNum = Viginere[Text[X].upper()]
      Helper = (LetterNum + Shift) % 26
      if Text[X].isupper():
        Lister.append(VFU[Helper])
      else:
        Lister.append(VFL[Helper])
    else:
      Lister.append(Text[X])
      Z -= 1
    X += 1
    Z += 1
  print(*Lister, sep = "")
elif Asker == 2:
  for i in range(len(Text)):
    Shift = Viginere[Key[(Z % len(Key))]]
    if Text[X].isalpha():
      LetterNum = Viginere[Text[X].upper()]
      Helper = (LetterNum - Shift) % 26
      if Text[X].isupper():
        Lister.append(VFU[Helper])
      else:
        Lister.append(VFL[Helper])
    else:
      Lister.append(Text[X])
      Z -= 1
    X += 1
    Z += 1
  print(*Lister, sep = "")

This will solve your problem easily, and it will even ask you for the input of the string and the key! It is the definition of python magic.

Upvotes: 0

m7mdbadawy
m7mdbadawy

Reputation: 920

You just need to add an else statement in translateMessage() to add the space to the output like this

def translateMessage(key, message, mode):
    translated = ""

    keyIndex = 0
    key = key.upper()

    for symbol in message:
        xyz = alphabet.find(symbol.upper())
        if xyz != -1:
            if mode == 'encrypt' or 'e':
                xyz += alphabet.find(key[keyIndex]) + 1
            elif mode == 'decrypt' or 'd':
                xyz -= alphabet.find(key[keyIndex]) + 1

            xyz %= len(alphabet)

            if symbol.isupper():
                translated += alphabet[xyz]
            elif symbol.islower():
                translated += alphabet[xyz].lower()

            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0
        else : translated += symbol #this will add space as it is

    return translated

Upvotes: 2

Related Questions