CodeCraft
  • Github
  • Instagram
< >

Cipher

TIME LIMIT = 5 SECS.

  • Indian army is going to do a surprise attack on one of its enemies country. The President of India, the Supreme Commander of the Indian Army
    will be sending an alert message to all its commanding centers. As enemy would be monitoring the message, Indian army is going to encrypt
    (cipher) the message using basic encryption technique. A decoding key 'K' (number) would be sent secretly. You are assigned to develop a cipher
    program to encrypt the message. Your cipher must rotate every character in the message by a fixed number making it unreadable by enemies.
    Given a single line of string 'S' containing alpha, numeric and symbols, followed by a number '0<=N<=1000'. Encrypt and print the resulting string.
    Note: The cipher only encrypts Alpha and Numeric. (A-Z, a-z, and 0-9) . All Symbols, such as - , ; %, remain unencrypted.
Input Output
The input consists of a string 'S' containing alpha, numeric and symbols, followed by a number '0<=N<=1000' Encrypt and print the resulting string.

Example Test Case
Input              Output
All-convoYs-9-be:Alert1.
4
Epp-gsrzsCw-3-fi:Epivx5.
Solution

#include <stdio.h> #include <string.h> int main() { char s[100000], op[100000]; int k, i; /* 's' = the message to be encrypted 'op' = encrypted message output 'k' = store shift value 'i' = looping variable */ // Accept the message to be encrypted. gets(s); // Accept the shift value (the value by which the characters are to be shifted). scanf("%d", & k); for (i = 0; i < strlen(s); i++) { //Check if the character is UPPERCASE or not if (s[i] >= 65 && s[i] <= 90) /* Check if the shifted value's ascii character is greater than 90 (which is invalid, because there is no UPPERCASE alphabet greater than Z) */ if ((s[i] + (k % 26)) > 90) /* If yes, subtract 90 from the obtained sum (which will give us the normal shift value), and add it to 64 because after Z, the cycle should begin again from A. */ op[i] = 64 + ((s[i] + (k % 26)) - 90); else // If no, then proceed normally op[i] = s[i] + (k % 26); } //Check if the character is lowercase or not else if (s[i] >= 97 && s[i] <= 122) { /* Check if the shifted value's ascii character is greater than 122 (which is invalid, because there is no lowercase alphabet greater than z). */ if ((s[i] + (k % 26)) > 122) /* If yes, subtract 122 from the obtained sum (which will give us the normal shift value), and add it to 96 because after Z, the cycle should begin again from a. */ op[i] = 96 + ((s[i] + (k % 26)) - 122); else // If no, then proceed normally op[i] = s[i] + (k % 26); } //Check if the character is a numeric digit or not. else if (s[i] >= 48 && s[i] <= 57) { /* Check if the shifted value's ascii character is greater than 57 (which is invalid, because there is no decimal digit greater than 9) */ if ((s[i] + (k % 10)) > 57) /* If yes, subtract 57 from the obtained sum (which will give us the normal shift value), and add it to 47 because after 9, the cycle should begin again from 0. */ op[i] = 47 + ((s[i] + (k % 10)) - 57); else // If no, proceed normally op[i] = s[i] + (k % 10); } else { //ignore (i.e don't shift) any other kind of character (which means special characters) op[i] = s[i]; } // Print the output (the encrypted message) puts(op); return 0; }
  • #1 Squared and cubic
  • #2 Digits
  • #3 Multiple Adder
  • #4 Grains in a Chessboard
  • #5 Age In Days
  • #6 Stay or Go
  • #7 Cipher

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();