Is there a way I could code a cipher that doesn't incorporated a space in the cipher?
Question:
I am coding a basic Caesar Cipher, which is almost finished, but has a small problem. When I run the code and it asks for the user’s input, if the inputs any spaces, the program will include them in the cipher. I want to just pass it without encrypting.
This is the code –
#This is a working code encrypter and decrypter
#Created by Aykhan Salimov on 08.23.2020
#"s" specifies the number of the shift to encrypt
s = 3
text = input("Input text that you wish to be ciphered: ")
def cipherText(text,s):
global result
result = ""
x = len(text) - text.count(" ")
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
print("Ciphertext:", result)
#This is the decrypter
#"d" is the amount need to shift back
d = s * -1
#a is the variable used to use the encrypted code
def plainText(text,d,result):
decrypt = ""
for i in range(len(result)):
char = result[i]
if char.isupper():
decrypt += chr((ord(char) + d - 65) % 26 + 65)
else:
decrypt += chr((ord(char) + d - 97) % 26 + 97)
print("Plaintext:", decrypt)
cipherText(text,s)
plainText(text,d,result)
This program does exactly what I want it to, but it isn’t correctly able to exclude spaces from the cipher. I am a beginner programmer, and I really need help on creating my first cipher.
Answers:
if what you want is to just pass it without encrypting it you can do the following
for i in range(len(text)):
char = text[i]
if char == " ":
result += " "
continue
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
or if what you want is to completely remove it you can do result += ""
.
If you want to remove spaces from input, You can use replace()
:
string = string.replace(" ","") #Replace space
Input:
Hi Aykhan
Output:
HiAykhan
Fixed your code:
We handle each of the three cases:
- Uppercase
- Lowercase
- Non-letters, such as spaces
What was the problem?
Lowercase and non-letters were treated in the same condition of:
if char.isupper():
<code>
# Here both space and lower-case were handeled
else:
<code>
Example:
Input text that you wish to be ciphered: Sabich dsF
Ciphertext: Vaelfk gvI
Plaintext: Sabich dsF
# This is a working code encrypter and decrypter
# Created by Aykhan Salimov on 08.23.2020
# "s" specifies the number of the shift to encrypt
s = 3
text = input("Input text that you wish to be ciphered: ")
def cipherText(text, s):
global result
result = ""
x = len(text) - text.count(" ")
for i in range(len(text)):
char = text[i]
if 'a' < char < 'z':
result += chr((ord(char) + s - 97) % 26 + 97)
elif 'A' < char < 'Z':
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += char
print("Ciphertext:", result)
# This is the decrypter
# "d" is the amount need to shift back
d = s * -1
# a is the variable used to use the encrypted code
def plainText(text, d, result):
decrypt = ""
for i in range(len(result)):
char = result[i]
if 'a' < char < 'z':
decrypt += chr((ord(char) + d - 97) % 26 + 97)
elif 'A' < char < 'Z':
decrypt += chr((ord(char) + d - 65) % 26 + 65)
else:
decrypt += char
print("Plaintext:", decrypt)
cipherText(text, s)
plainText(text, d, result)
I am coding a basic Caesar Cipher, which is almost finished, but has a small problem. When I run the code and it asks for the user’s input, if the inputs any spaces, the program will include them in the cipher. I want to just pass it without encrypting.
This is the code –
#This is a working code encrypter and decrypter
#Created by Aykhan Salimov on 08.23.2020
#"s" specifies the number of the shift to encrypt
s = 3
text = input("Input text that you wish to be ciphered: ")
def cipherText(text,s):
global result
result = ""
x = len(text) - text.count(" ")
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
print("Ciphertext:", result)
#This is the decrypter
#"d" is the amount need to shift back
d = s * -1
#a is the variable used to use the encrypted code
def plainText(text,d,result):
decrypt = ""
for i in range(len(result)):
char = result[i]
if char.isupper():
decrypt += chr((ord(char) + d - 65) % 26 + 65)
else:
decrypt += chr((ord(char) + d - 97) % 26 + 97)
print("Plaintext:", decrypt)
cipherText(text,s)
plainText(text,d,result)
This program does exactly what I want it to, but it isn’t correctly able to exclude spaces from the cipher. I am a beginner programmer, and I really need help on creating my first cipher.
if what you want is to just pass it without encrypting it you can do the following
for i in range(len(text)):
char = text[i]
if char == " ":
result += " "
continue
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
or if what you want is to completely remove it you can do result += ""
.
If you want to remove spaces from input, You can use replace()
:
string = string.replace(" ","") #Replace space
Input:
Hi Aykhan
Output:
HiAykhan
Fixed your code:
We handle each of the three cases:
- Uppercase
- Lowercase
- Non-letters, such as spaces
What was the problem?
Lowercase and non-letters were treated in the same condition of:
if char.isupper():
<code>
# Here both space and lower-case were handeled
else:
<code>
Example:
Input text that you wish to be ciphered: Sabich dsF
Ciphertext: Vaelfk gvI
Plaintext: Sabich dsF
# This is a working code encrypter and decrypter
# Created by Aykhan Salimov on 08.23.2020
# "s" specifies the number of the shift to encrypt
s = 3
text = input("Input text that you wish to be ciphered: ")
def cipherText(text, s):
global result
result = ""
x = len(text) - text.count(" ")
for i in range(len(text)):
char = text[i]
if 'a' < char < 'z':
result += chr((ord(char) + s - 97) % 26 + 97)
elif 'A' < char < 'Z':
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += char
print("Ciphertext:", result)
# This is the decrypter
# "d" is the amount need to shift back
d = s * -1
# a is the variable used to use the encrypted code
def plainText(text, d, result):
decrypt = ""
for i in range(len(result)):
char = result[i]
if 'a' < char < 'z':
decrypt += chr((ord(char) + d - 97) % 26 + 97)
elif 'A' < char < 'Z':
decrypt += chr((ord(char) + d - 65) % 26 + 65)
else:
decrypt += char
print("Plaintext:", decrypt)
cipherText(text, s)
plainText(text, d, result)