Here it is better explained by a diagram:
Now for the code in python 2.7, Firstly we will create a function for encoding so open a new file:
def encode(text)
Create an array to hold the new characters:
new_text = []
Now we need to loop through each character in the input text the user will give us:
for char in text:
ord
converts a character to its numbered value in ascii.In this case we will shift all the letters by 3 forward in the ascii table however you can use whatever number you want as long as it lower than 127:
char = ord(char) + 3
To make sure that we stay in the range of ASCII values we need to implement an if statement
if(char > 127):
char = char - 127
Now to convert the value back to a character:
char = chr(char)
Then add this character to the array:
new_text.append(char)
We have reached the end of this function and now have to return the array to the program:
return new_array
You should now have your first function, which put together look like this:
def encode(text):
new_text = []
for char in text:
char = ord(char) + 3
if(char > 127):
char = char - 127
char = chr(char)
new_text.append(char)
return new_text
The decode function works the same you just need to the opposite to decode instead of encode. I have made the changes in bold below.
def decode(text):
new_text = []
for char in text:
char = ord(char) - 3
if(char < 0):
char = char + 127
char = chr(char)
new_text.append(char)
return new_text
We now need to allow the user to input some text and decide to encode or decode.
This can be done with a simple while loop and if/elif statment:
choice = 0
while choice != 3:
choice = raw_input("1:Encode, 2:Decode, 3:Exit ")
if(choice == "1"):
user_input = raw_input("Enter a word: ")
new_text = encode(user_input)
print ''.join(new_text)
elif(choice == "2"):
user_input = raw_input("Enter a word: ")
new_text = decode(user_input)
print ''.join(new_text)
elif(choice == "3"):
print "Goodbye"
else:
print "Please enter a correct choice"
Now lets see if it works
Checking the encoding works:
Choose 1 for encode then enter "hello, world" it should give the output of "khoor/#zruog"
Checking the decoding works:
Choose 2 for decode then enter "khoor/#zruog" it should give the output of "hello, world"
Awesome..You have clearly explained ...Its very useful for me to know about new things..Keep on blogging..
ReplyDeleteSEO training in chennai
ReplyDeleteThis content of information has
helped me a lot. It is very well explained and easy to understand.
seo training classes
seo training course
seo training institute in chennai
seo training institutes
seo courses in chennai
seo institutes in chennai
seo classes in chennai
seo training center in chennai
Tally ERP 9 Training
tally classes
Tally Training institute in Chennai
Tally course in Chennai
This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article.
ReplyDeleteLearn Best Digital Marketing Course in Chennai
Digital Marketing Course Training with Placement in Chennai
Learn Digital Marketing Course Training in Chennai
Digital Marketing Training with Placement Institute in Chennai
Best Big Data Course Training with Placement in Chennai
Big Data Analytics and Hadoop Course Training in Chennai
Best Data Science Course Training with Placement in Chennai
Data Science Online Certification Course Training in Chennai
Learn Best Android Development Course Training Institute in Chennai
Android Application Development Programming Course Training in Chennai
Learn Best AngularJS 4 Course Online Training and Placement Institute in Chennai
Learn Seo Course Training Institute in Chennai
Learn Social Media Marketing Training with Placement Institute in Chennai
Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
ReplyDeleteDigital Marketing Course In Kolkata
Web Design Course In Kolkata
SEO Course In Kolkata