Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def caesar(start_text, shift_amount, direction):
  end_text = ""
  if direction == "decode":
    shift_amount *= -1
  for char in start_text:
    if char not in alphabet:
      new_char = char
    else:
      position = alphabet.index(char)
      new_position = position + shift_amount
      if new_position > len(alphabet) - 1:
        new_position -= len(alphabet)
      elif new_position < 0:
        new_position += len(alphabet)
      new_char = alphabet[new_position]
    end_text += new_char

  print(f"Here's the {direction}d result: {end_text}")
  restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
  if restart == "yes":
    cipher_program()
  else:
    print("Goodbye")

def cipher_program():
  direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
  text = input("Type your message:\n").lower()
  shift = int(input("Type the shift number:\n"))
  shift = shift % len(alphabet)
  caesar(text, shift, direction)

cipher_program()


What I have tried:

i have tried to code ..though i dont know how to code in python thats why i didnt get it
Posted
Updated 9-Feb-21 1:47am
v2

from "The Code Conversion Service": :-)

C++
#include<iostream>
  
using  namespace std;

string caesar( string message, int shift)
{
  string result;
  for (auto c : message)
  {
    if ( c >= 'a' && c <='z')
      c = (c + shift - 'a') % ('z' - 'a' + 1) + 'a';
    result += c;
  }
  return result;
}

int main()
{
  string operation, message;
  int shift;

  cout << "Type 'decode', otherwise it will encode\n";
  getline(cin,operation);
  cout << "Type the message\n";
  getline(cin, message);
  cout << "Type the shift amount\n";
  cin >> shift;

  cout << caesar( message, (operation == "decode" ? - shift : shift));
}
 
Share this answer
 
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And if I'm being totally honest, if you can't write that trivial code for yourself, you should think seriously about abandoning your course, and perhaps considering a career in fast food ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900