How do I write code to decode a string?

by on October 8, 2011

Q: def main():

#read input <string> from user

#convert <string> to all uppercase

#for each <char>acter in <string>

#if <char> is not a space

#convert <char> to its ASCII <value>

#rotate <value> by a given amount

#convert <value> back to a character, <char>

#print out <char>
but they want us to decode the alphabetical letters to look like the alphabets above.i am confuse a bit.do i have to use if and for statements.i know i have to use chr and ord to convert.please i need help

i have it on emacs but cannot copy to this word file.i have my string of the alphabetical letters but how to decode to be in the form
BJ YMJ UJTUQJ TK YMJ ZSNYJI XYFYJX, NS TWIJW YT KTWR F RTWJ UJWKJHY ZSNTS, JXYFGQNXM OZXYNHJ, NSXZWJ ITRJXYNH YWFSVZNQNYD, UWTANIJ KTW YMJ HTRRTS IJKJSXJ, UWTRTYJ YMJ LJSJWFQ BJQKFWJ, FSI XJHZWJ YMJ GQJXXNSLX TK QNGJWYD YT TZWXJQAJX FSI TZW UTXYJWNYD, IT TWIFNS FSI JXYFGQNXM YMNX HTSXYNYZYNTS KTW YMJ ZSNYJI XYFYJX TK FRJWNHF


2 Responses to “How do I write code to decode a string?”
  1.  

    #reads input string
    input_string = input (“Enter input string: “)
    #converts input string to all uppercase
    uppercase_string = str.upper(input_string)
    rotation_value = 1
    for ch in uppercase_string:
    if ch != ‘ ‘:
    ascii_value = ord(ch)
    rotate_value = ((ascii_value – ord(‘A’)) + rotation_value)%26 + ord(‘A’)
    rotate_char = chr(rotate_value)
    print(rotate_char)

     
  2.  

    also does this program calculate the correct shift amount by finding the letter used most often in the coded text and using the information in the letter frequencies chart shown below.