Caesar Cipher

Here given code implementation process.

/*
  Java program for
  Caesar Cipher
*/
public class CaesarCipher
{
	String encryptText(String text, int shift)
	{
		String output = "";
		// Execute loop through by length of given text
		for (int i = 0; i < text.length(); ++i)
		{
			if (text.charAt(i) >= 'A' && 
                text.charAt(i) <= 'Z')
			{
				// When character is uppercase letter
				output = output + (char)(
                  (int)((text.charAt(i) + shift - 65) % 26) + 65
                );
			}
			else
			{
				output = output + (char)(
                  (int)((text.charAt(i) + shift - 97) % 26) + 97
                );
			}
		}
		return output;
	}
	public static void main(String[] args)
	{
		CaesarCipher task = new CaesarCipher();
		String text = "NEWiceCream";
		int shift = 12;
		String encrypt = task.encryptText(text, shift);
		// Display result
		System.out.println(" Given text : " + text);
		System.out.println(" Encrypt text : " + encrypt);
	}
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
  C++ program for
  Caesar Cipher
*/
class CaesarCipher
{
	public: string encryptText(string text, int shift)
	{
		string output = "";
		// Execute loop through by length of given text
		for (int i = 0; i < text.length(); ++i)
		{
			if (text[i] >= 'A' && text[i] <= 'Z')
			{
				// When character is uppercase letter
				output = output  +  (char)(
                  (int)((text[i] + shift - 65) % 26) + 65
                );
			}
			else
			{
				output = output  +  (char)(
                  (int)((text[i] + shift - 97) % 26) + 97
                );
			}
		}
		return output;
	}
};
int main()
{
	CaesarCipher *task = new CaesarCipher();
	string text = "NEWiceCream";
	int shift = 12;
	string encrypt = task->encryptText(text, shift);
	// Display result
	cout << " Given text : " << text << endl;
	cout << " Encrypt text : " << encrypt << endl;
	return 0;
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
// Include namespace system
using System;
/*
  Csharp program for
  Caesar Cipher
*/
public class CaesarCipher
{
	String encryptText(String text, int shift)
	{
		String output = "";
		// Execute loop through by length of given text
		for (int i = 0; i < text.Length; ++i)
		{
			if (text[i] >= 'A' && text[i] <= 'Z')
			{
				// When character is uppercase letter
				output = output + (char)(
                  (int)((text[i] + shift - 65) % 26) + 65
                );
			}
			else
			{
				output = output + (char)(
                  (int)((text[i] + shift - 97) % 26) + 97
                );
			}
		}
		return output;
	}
	public static void Main(String[] args)
	{
		CaesarCipher task = new CaesarCipher();
		String text = "NEWiceCream";
		int shift = 12;
		String encrypt = task.encryptText(text, shift);
		// Display result
		Console.WriteLine(" Given text : " + text);
		Console.WriteLine(" Encrypt text : " + encrypt);
	}
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
package main


import "fmt"
/*
  Go program for
  Caesar Cipher
*/

func encryptText(text string, shift int) string {
	var output string = ""
	// Execute loop through by length of given text
	for i := 0 ; i < len(text) ; i++ {
		if text[i] >= 'A' && text[i] <= 'Z' {
			// When character is uppercase letter
			output = output + string(((int(text[i]) + shift - 65) % 26) + 65) 
		} else {
			output = output + string(((int(text[i]) + shift - 97) % 26) + 97)  
		}
	}
	return output
}
func main() {
	var text string = "NEWiceCream"
	var shift int = 12
	var encrypt string = encryptText(text, shift)
	// Display result
	fmt.Println(" Given text : ", text)
	fmt.Println(" Encrypt text : ", encrypt)
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
<?php
/*
  Php program for
  Caesar Cipher
*/
class CaesarCipher
{
	function encryptText($text, $shift)
	{
		$output = "";
		// Execute loop through by length of given text
		for ($i = 0; $i < strlen($text); ++$i)
		{
			if ($text[$i] >= 'A' && $text[$i] <= 'Z')
			{
				// When character is uppercase letter
				$output = $output.strval(chr(
                  ((int)((ord($text[$i]) + $shift - 65) % 26) + 65)
                ));
			}
			else
			{
				$output = $output.strval(
                  chr(
                    ((int)((ord($text[$i]) + $shift - 97) % 26) + 97)
                  )
                );
			}
		}
		return $output;
	}
}

function main()
{
	$task = new CaesarCipher();
	$text = "NEWiceCream";
	$shift = 12;
	$encrypt = $task->encryptText($text, $shift);
	// Display result
	echo(" Given text : ".$text.
		"\n");
	echo(" Encrypt text : ".$encrypt.
		"\n");
}
main();

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
/*
  Node JS program for
  Caesar Cipher
*/
class CaesarCipher
{
	encryptText(text, shift)
	{
		var output = "";
		// Execute loop through by length of given text
		for (var i = 0; i < text.length; ++i)
		{
			if (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z')
			{
				// When character is uppercase letter
				output = output + String.fromCharCode(
                  parseInt((text.charCodeAt(i) + shift - 65) % 26) + 65
                );
			}
			else
			{
				output = output + String.fromCharCode(
                  parseInt((text.charCodeAt(i) + shift - 97) % 26) + 97
                );
			}
		}
		return output;
	}
}

function main()
{
	var task = new CaesarCipher();
	var text = "NEWiceCream";
	var shift = 12;
	var encrypt = task.encryptText(text, shift);
	// Display result
	console.log(" Given text : " + text);
	console.log(" Encrypt text : " + encrypt);
}
main();

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
#  Python 3 program for
#  Caesar Cipher
class CaesarCipher :
	def encryptText(self, text, shift) :
		output = ""
		i = 0
		#  Execute loop through by length of given text
		while (i < len(text)) :
			if (text[i] >= 'A'
				and text[i] <= 'Z') :
				#  When character is uppercase letter
				output = output + str(chr(
                  (int)((ord(text[i]) + shift - 65) % 26) + 65
                ))
			else :
				output = output + str(chr(
                  (int)((ord(text[i]) + shift - 97) % 26) + 97
                ))
			
			i += 1
		
		return output
	

def main() :
	task = CaesarCipher()
	text = "NEWiceCream"
	shift = 12
	encrypt = task.encryptText(text, shift)
	#  Display result
	print(" Given text : ", text)
	print(" Encrypt text : ", encrypt)

if __name__ == "__main__": main()

Output

 Given text :  NEWiceCream
 Encrypt text :  ZQIuoqOdqmy
#  Ruby program for
#  Caesar Cipher
class CaesarCipher 
	def encryptText(text, shift) 
		output = ""
		i = 0
		#  Execute loop through by length of given text
		while (i < text.length) 
			if (text[i] >= 'A' && text[i] <= 'Z') 
				#  When character is uppercase letter
				output = output +(
                  ((text[i].ord + shift - 65) % 26) + 65
                ).chr.to_s
			else
 
				output = output +(
                  ((text[i].ord + shift - 97) % 26) + 97
                ).chr.to_s
			end

			i += 1
		end

		return output
	end

end

def main() 
	task = CaesarCipher.new()
	text = "NEWiceCream"
	shift = 12
	encrypt = task.encryptText(text, shift)
	#  Display result
	print(" Given text : ", text, "\n")
	print(" Encrypt text : ", encrypt, "\n")
end

main()

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
import scala.collection.mutable._;
/*
  Scala program for
  Caesar Cipher
*/
class CaesarCipher()
{
	def encryptText(text: String, shift: Int): String = {
		var output: String = "";
		var i: Int = 0;
		// Execute loop through by length of given text
		while (i < text.length())
		{
			if (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z')
			{
				// When character is uppercase letter
				output = output + (
                  ((text.charAt(i).toInt + shift - 65) % 26).toInt 
                  + 65).toChar.toString();
			}
			else
			{
				output = output + (
                  ((text.charAt(i).toInt + shift - 97) % 26).toInt 
                  +
                  97).toChar.toString();
			}
			i += 1;
		}
		return output;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CaesarCipher = new CaesarCipher();
		var text: String = "NEWiceCream";
		var shift: Int = 12;
		var encrypt: String = task.encryptText(text, shift);
		// Display result
		println(" Given text : " + text);
		println(" Encrypt text : " + encrypt);
	}
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy
import Foundation;
/*
  Swift 4 program for
  Caesar Cipher
*/
class CaesarCipher
{
	func encryptText(_ data: String, _ shift: Int) -> String
	{
		var output: String = "";
		var i: Int = 0;
      	let text = Array(data);
      	var j = 0;
      	var k = 0;
		// Execute loop through by length of given text
		while (i < text.count)
		{
	

			j = Int(UnicodeScalar(String(text[i]))!.value);

			if (text[i] >= "A" && text[i] <= "Z")
			{
				// When character is uppercase letter
				k = ( (j + shift - 65) % 26 ) + 65 ;
			}
			else
			{
				k = ( (j + shift - 97) % 26 ) + 97 ;
			}
			
			let s = String(format: "%c", k) as String

			output = output + s;

			i += 1;
		}
		return output;
	}
}
func main()
{
	let task: CaesarCipher = CaesarCipher();
	let text: String = "NEWiceCream";
	let shift: Int = 12;
	let encrypt: String = task.encryptText(text, shift);
	// Display result
	print(" Given text : ", text);
	print(" Encrypt text : ", encrypt);
}
main();

Output

 Given text :  NEWiceCream
 Encrypt text :  ZQIuoqOdqmy
/*
  Kotlin program for
  Caesar Cipher
*/
class CaesarCipher
{
	fun encryptText(text: String, shift: Int): String 
	{
		var output: String = "";
		var i: Int = 0;
		// Execute loop through by length of given text
		while (i < text.length)
		{
			if (text.get(i) >= 'A' && text.get(i) <= 'Z')
			{
				// When character is uppercase letter
				output = output + 
                  (((text.get(i).toInt() + shift - 65) % 26).toInt() + 
                   65).toChar().toString();
			}
			else
			{
				output = output + 
                  (((text.get(i).toInt() + shift - 97) % 26).toInt() + 
                   97).toChar().toString();
			}
			i += 1;
		}
		return output;
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CaesarCipher = CaesarCipher();
	val text: String = "NEWiceCream";
	val shift: Int = 12;
	val encrypt: String = task.encryptText(text, shift);
	// Display result
	println(" Given text : " + text);
	println(" Encrypt text : " + encrypt);
}

Output

 Given text : NEWiceCream
 Encrypt text : ZQIuoqOdqmy

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved