Convert this C++ code to Java code this code is to encrypt and decrypt strings o
ID: 3863731 • Letter: C
Question
Convert this C++ code to Java code
this code is to encrypt and decrypt strings of characters using Caesar cipher
please attach samples run of the code
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
#define MAX_SIZE 200
void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string.
void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string.
using namespace std;
int main()
{
char input_str[MAX_SIZE];
int shift = 6; // This is the Caesar encryption key... You can change it to whatever value you wish!
system("cls"); // System call to clear the console screen!
cout<<" Enter the string to be encrypted : ";
gets(input_str); // Getting the user to input the string to be encrypted!
cout<<" Original string: " << input_str;
// Function call to encrypt the input string
encrypt_str(input_str, shift);
return 0;
}
// Function Definition for the function to encrypt the input string
void encrypt_str(char xyz[], int key){
char crypted_str[MAX_SIZE]; // To store the resulting string
int k=0; // For indexing purpose
char str;
while(xyz[k] !='') // Processing each character of the string until the "end of line" character is met
{
str = toupper(xyz[k]); // Remove "toupper" from this line if you don't wish to see the
// result string in Uppercase...
if(str != ' ') str += key;
{
if(str > 'z') str -=26;
{
crypted_str[k] = str;
k++;
}
}
}
crypted_str[k]='';
cout << " Encrypted string is: " << crypted_str; // Displaying the Crypted String
// Function call to decrypt the encrypted string
decrypt_str(crypted_str, key);
}
// Function Definition for the function to decrypt the encrypted string.
void decrypt_str(char xyz[], int key){
char decrypted_str[MAX_SIZE];
char str;
int k=0;
while(xyz[k] !='')
{
str = xyz[k];
if(str != ' ') str -= key;
{
if(str < 'A' && str !=' ') str += 26;
{
decrypted_str[k] = str;
k++;
}
}
}
decrypted_str[k]='';
cout << " Decrypted string is: " << decrypted_str;
}
Explanation / Answer
package encryptdecrypt;
import java.util.Scanner;
public class EncryptDecrypt {
int MAX_SIZE = 200;
public void encrypt_str(char xyz[], int key)
{
char crypted_str[] = new char[MAX_SIZE];
int k=0;
char str = ' ';
while(xyz[k] != ''){
str = Character.toUpperCase(xyz[k]);
System.out.println("str = "+ str);
if(str != ' ')
str += key;
if(str > 'Z')
str -= 26;
crypted_str[k] = str;
k++;
}
crypted_str[k] = '';
System.out.println(" Encrypted string is: "+crypted_str);
decrypt_str(crypted_str,key);
}
public void decrypt_str(char xyz[], int key){
char decrypted_str[] = new char[MAX_SIZE];
char str;
int k = 0;
while(xyz[k] != ''){
str = xyz[k];
if(str != ' ') str -= key;
if(str < 'A' && str != ' ') str += 26;
{
decrypted_str[k] =str;
k++;
}
decrypted_str[k] = '';
System.out.println(" Decrypted string is: "+decrypted_str);
}
}
public static void main(String[] args) {
EncryptDecrypt ed = new EncryptDecrypt();
Scanner sc = new Scanner(System.in);
char input_str[] = new char[200];
int shift = 6;
System.out.println("Enter the string to be enrypted: ");
String str = sc.nextLine();
System.out.println("Original string: "+str);
input_str = str.toCharArray();
ed.encrypt_str(input_str,shift);
}
}