Python Forum
Can someone please help me convert this simple C ROT cipher code to Python code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please help me convert this simple C ROT cipher code to Python code?
#1
Hi!
I'm having difficulties converting this simple C code into Python code. I don't code in Python much, I'm only using it for Flask web application and I've been struggling to find help online, since all I can find is limited to alphabet shifting.

Basically, I need a code that shifts ASCII value of all characters in string array, no matter if they're valid alphabet letters.
So the C code looks like this:

[Sorry for using Python code formatting, I can't find an option for general code block]
#/* 'str[]' is character array, 'shift' is the number by which the ASCII value will be shifted */  
void ROTciph(char str[], int shift) {
    unsigned int i;
    for(i=0; i<strlen(str); ++i)
    {
          str[i] = str[i] + shift;
    }
}
I'd be very grateful if someone could help me 'translating' this into a Python code.
Thanks!

PS: I know this is a very beginner task, but time is playing against me, so once again, thanks for your help!
Reply
#2
This forum is focused on Python education, which means that we need to see an attempt from you first. Along with that, you should include a description of the specific problem which is blocking you from completing your task yourself.
Reply
#3
Sorry, I'll try someplace else. As I said, my specific problem is that my Python knowledge is close to absolute zero. Thanks and have a nice day!
Reply
#4
The first thing you should do is define some input/outputs. Some test strings, and what you expect the rotated values to be. Does "z" become 'm' or 'M', for example. What does whitespace rotate to? Digits?

From there, the string module is probably the next best place to look.
>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Reply
#5
(Jun-13-2019, 09:51 PM)nilamo Wrote: The first thing you should do is define some input/outputs. Some test strings, and what you expect the rotated values to be. Does "z" become 'm' or 'M', for example. What does whitespace rotate to? Digits? From there, the string module is probably the next best place to look.
>>> import string >>> dir(string) ['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace'] >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Thank you! I figured I just needed to shift ord() values of all characters in the given string and it works!

Thanks for your time!
Reply
#6
from collections import deque
import string

# original letters
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase

# copy of original letters as deque
lowercase_rot13 = deque(lowercase)
uppercase_rot13 = deque(uppercase)

# roate the deque by 13
lowercase_rot13.rotate(13)
uppercase_rot13.rotate(13)

# combine the lowercase and uppercase
charset = lowercase + uppercase
charset_rot13 = (lowercase_rot13 + uppercase_rot13)

# use join, to convert the deque back to a str
charset_rot13 = ''.join(charset_rot13)

# make the translation
encode_rot13 = str.maketrans(charset, charset_rot13)
decode_rot13 = str.maketrans(charset_rot13, charset)

# test
msg = 'Hello World'
enc = msg.translate(encode_rot13)
dec = enc.translate(decode_rot13)
print(msg)
print(enc)
print(dec)
The encode_rot13 object is a dict.
The method str.translate uses this dict.
And sorted:
We can make this dict with one line:
encode_rot13 = {char: char + 13 for char in range(65, 122 + 1)}
decode_rot13 = {v: k for k, v in encode_rot13.items()} # just swapping keys with values
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 415 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
  Why can I not see the code correctly in Python IDLE.? Trump 8 656 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
  Help with simple code JacobSkinner 1 309 Mar-18-2024, 08:08 PM
Last Post: deanhystad
Sad Selenium update broke python code genericusername12414 1 230 Mar-16-2024, 07:33 PM
Last Post: snippsat
  Algorithm for extracting comments from Python source code Pavel1982 6 506 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Python best library for Excel reports & review of existing code MasterOfDestr 4 607 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
Lightbulb python code debuging yunus 1 331 Feb-11-2024, 03:48 PM
Last Post: deanhystad
  Python code to set column width 1418 11 1,158 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python code for alignment and font size 1418 0 305 Jan-14-2024, 03:56 AM
Last Post: 1418
Question Rsa Cipher Paragoon2 3 620 Nov-27-2023, 12:30 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020