Binary - Case Sensitive Decryption

Binary - Case Sensitive Decryption 

PROBLEM STATEMENT :

The program must accept a string S representing an encrypted message as the input. The program must decrypt the string S and print the message based on the following conditions. 

- The program must split the string S into substrings of equal length 5

- For each substring, the program must form a binary representation by replacing the lower case alphabets with 0 and the upper case alphabets with 1

Then the program must find the decimal equivalent of each binary representation and replace them with the characters as given below. From 0 to 25 -> a to z. 

26 -> . (dot) 

27 -> , (comma) 

28 -> a SPACE character 

29 -> ? (question mark symbol) 

30 -> ' (single quote) 

31 -> " (double quote) 

Note: The string S contains only alphabets and its length is always a multiple of 5. 

Boundary Condition(s): 

1 <= Length of S <= 1000 

Input Format: 

The first line contains S. 

Output Format: 

The first line contains the decrypted string based on the given conditions. 


Example Input/Output 1: 
Input: () 
abcDEtSAYtmaRKs 
Output: dog

Explanation: 
The given string is abcDEtSAYtmaRKs. 
The 1st substring is abcDE. The binary representation is 00011. 3 -> d. 
The 2nd substring is tSAYt. The binary representation is 01110. 14 -> o. 
The 3rd substring is maRKs. The binary representation is 00110. 6 -> g. 
So the decrypted string is dog, which is printed as the output. 

Example Input/Output 2: 
Input: () 
EytWGeqNKYcbNpqLSXcoAHCUIbgrarZzcpRPvoYGYOQIUQAsHh
Output: the "art".




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> #include<ctype.h> //For isupper function #include<string.h> //For strcpy function #include<math.h> //For pow function int decimal(char* s) { int sum=0,n=4; for(int i=4;i>=0;--i) { if(isupper(s[i])) sum+=(int)pow(2,(n-i)); } return sum; } int main() { char s[10001],ch[]={'.',',',' ','?','\'','"'}; int l,i,d; scanf("%s%n",s,&l); //%n is used to calculate the length for(i=0;i<l;i+=5) { char r[10001]; strcpy(r,s); r[i+l]='\0'; d=decimal(r+i); if(d<=25) printf("%c",'a'+d); else printf("%c",ch[d-26]); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int i; char ch[]={'.',',',' ','?','\'','"'}; string s,res=""; cin>>s; for(i=0;i<s.size();++i) { if(islower(s[i])) s[i]='0'; else s[i]='1'; } for(i=0;i<s.size();i+=5) { int d=stoi(s.substr(i,5),0,2); if(d<=25) res+='a'+d; else res+=ch[d-26]; } cout<<res; }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); char[] s=sc.next().toCharArray(); char[] ch={'.',',',' ','?','\'','"'}; String res=""; int i; for(i=0;i<s.length;++i) { if(Character.isLowerCase(s[i])) s[i]='0'; else s[i]='1'; } for(i=0;i<s.length;i+=5) { int d=Integer.parseInt(new String(s).substring(i,i+5),2); if(d<=25) res+=(char)('a'+d); else res+=ch[d-26]; } System.out.println(res); } }



PYTHON

       

s=input().strip() l=len(s) res='' lst=[".",","," ","?","'","\""] for i in range(0,l,5): r=list(s[i:i+5]) for j in range(5): if r[j].islower(): r[j]='0' else: r[j]='1' d=int(''.join(r),2) if d<=25: res+=chr(97+d) else: res+=lst[d-26] print(res)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right