Secret Message - 2
Given a string, write a program to print a secret message that replaces characters with numbers 'a' with 1, 'b' with 2, ..., 'z' with 26 where characters are separated by '-'.
Note: You need to replace both uppercase and lowercase characters. You can ignore replacing all characters that are not letters.
a b c d e f g h i j
1 2 3 4 5 6 7 8 9 10
k l m n o p q r
11 12 13 14 15 16 17 18
s t u v w x y z
19 20 21 22 23 24 25 26
def main():
#Read string
inputString = input("Enter string: ")
outputString=""
for letter in inputString:
number=getNumber(letter)
if(number!=-1):
outputString+=str(number)+"-"
else:
outputString+=letter
print(outputString[:-1])
#This function convert letter to number
def getNumber(letter):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(0,len(alphabet)):
if letter.islower() and letter==alphabet[i]:
return i+1
if letter.isupper() and letter==alphabet[i].upper():
return i+1
return -1
main()
Comments
Leave a comment