Caesar Cipher
Problem Statement :
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. Original alphabet: abcdefghijklmnopqrstuvwxyz Alphabet rotated +3: defghijklmnopqrstuvwxyzabc Function Description Complete the caesarCipher function in the editor below. caesarCipher has the following parameter(s): string s: cleartext int k: the alphabet rotation factor Returns string: the encrypted string Input Format The first line contains the integer, n, the length of the unencrypted string. The second line contains the unencrypted string, s. The third line contains k, the number of letters to rotate the alphabet by. Constraints 1 <= n <= 100 0 <= k <= 100 s is a valid ASCII string without any spaces.
Solution :
Solution in C :
In C++ :
#include <iostream>
#include <string>
using namespace std;
int main() {
int N = 0, K = 0;
string str, dummy;
cin >> N; getline(cin, dummy);
getline(cin, str);
cin >> K;
int len = str.length();
for (int i = 0; i < len; ++i)
{
if (65 <= str[i] && str[i] <= 90)
str[i] = char(65 + ((str[i] - 65) + K) % 26);
else if (97 <= str[i] && str[i] <= 122)
str[i] = char(97 + ((str[i] - 97) + K) % 26);
}
cout << str << endl;
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int len = s.nextInt(); s.nextLine();
String str = s.nextLine();
int shift = s.nextInt();
char sarr[] = str.toCharArray();
for (int i=0; i<sarr.length; i++) {
sarr[i] = cryptIt(sarr[i], shift);
}
System.out.println(new String(sarr));
}
public static char cryptIt(char c, int shift) {
if (!Character.isAlphabetic(c)) return c;
char base = 'A';
if (c >= 'a') base = 'a';
return (char)(((c - base + shift) % 26) + base);
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,j,k;
char ar[101];
unsigned char x;
scanf("%d",&n);
scanf("%s",ar);
scanf("%d",&k);
for(i=0;i<n;i++)
{
x=ar[i];
if(x>=97 && x<=122)
{
x=x+(k%26);
if(x>122)
{
x=96+(x-122);
}
ar[i]=x;
}
else if(x>=65 && x<=90)
{
x=x+(k%26);
if(x>90)
{
x=64+(x-90);
}
ar[i]=x;
}
}
printf("%s",ar);
return 0;
}
In Python3 :
def main():
l = input()
st = input()
s = int(input())
l_s = [chr(i) for i in range(ord('a'),ord('z')+1)]
l_b = [chr(i) for i in range(ord('A'),ord('Z')+1)]
cpy = ""
for i in st:
if i in l_s:
cpy += l_s[(l_s.index(i)+s)%26]
elif i in l_b:
cpy += l_b[(l_b.index(i)+s)%26]
else:
cpy += i
print (cpy)
if __name__ == "__main__":
main()
View More Similar Problems
Delete a Node
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo
View Solution →Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →