Homepage Forums Dev & Debugging Spaces are not recognized

  • Spaces are not recognized

  • Pradipta Sarkar

    Member
    March 30, 2021 at 12:43 pm
    164 Coins
    24 Gems
    Level: Rookie

    #A python program to illustrate Caesar Cipher Technique

    def encrypt(text,s):

    result = ""

    # traverse text

    for i in range(len(text)):

    char = text[i]

    # Encrypt uppercase characters

    if (char.isupper()):

    result += chr((ord(char) + s-65) % 26 + 65)

    # Encrypt lowercase characters

    else:

    result += chr((ord(char) + s - 97) % 26 + 97)

    return result

    #check the above function

    text = "Hello World"

    s = 4

    print (text)

    print (encrypt(text,s)


    #Output

    Lippsrasvph

  • Satya Mukherjee

    Administrator
    March 30, 2021 at 8:15 pm
    8836 Coins
    484 Gems
    Helpful
    Up
    31
    Down
    Not Helpful

    Spaces are not defined in your program since you didn’t mention what to do with it.

    for i in range(len(text)):
    char = text[i]
    if (char.isupper()):
    result += chr((ord(char) + s - 65) % 26 + 65)
    elif (char.islower()):
    result += chr((ord(char) + s - 97) % 26 + 97)
    elif (char.isdigit()):
    result += str(int(char) + s)
    elif (char == '-'):
    result += '-'
    elif (char.ispace()):
    result += ''
    return result
    • This reply was modified 3 months, 4 weeks ago by  Satya Mukherjee. Reason: corrected indentation
    • This reply was modified 3 months, 4 weeks ago by  Satya Mukherjee.
    • Pradipta Sarkar

      Member
      April 1, 2021 at 11:30 am
      164 Coins
      24 Gems
      Level: Rookie
      Helpful
      Up
      0
      Down
      Not Helpful

      Thank you.

      elif (char.isspace()):
      result += " "

      Is what I was missing.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now

New Report

Close