Program to Encrypt a String using ! and @ - GeeksforGeeks

03 Jun, 2021
Given a string, the task is to encrypt this string using ! and @ symbols, alternatively. While encrypting the message the encrypted format must repeat the symbol as many times as the letter position in Alphabetical order.Examples:  
Input: string = "Ab" 
Output: !@@
Explanation:
Position of 'A' in alphabetical order is 1
and in String is odd position 
so encrypted message will have 1 '!'

Position of 'b' in alphabetical order is 2
and in String is even position 
so encrypted message will have 2 '@'

Therefore, the output "!@@"

Input: string = "CDE"
Output: !!!@@@@!!!!!
 
Approach: This is a very basic and simple type of Encryption technique and can be done as follows:  
Get the character one by one from the String
For each character, get the difference between the ASCII value of that character and ‘A'(if the character is a capital letter) or ‘a’ (if the letter is a small letter). This will be the number of times the encryption character is to be repeated.
For the ith character of the string, if i is odd, the encryption character will be ‘!’ and if i is even, the encryption character will be ‘@’.
Below is the implementation of the above code:  
C++
C
Java
Python3
C#
Javascript
// C++ program to Encrypt the String// using ! and @#include <bits/stdc++.h>using namespace std; // Function to encrypt the stringvoid encrypt(char input[100]){     // evenPos is for storing encrypting    // char at evenPosition    // oddPos is for storing encrypting    // char at oddPosition    char evenPos = '@', oddPos = '!';     int repeat, ascii;     for (int i = 0; i <= strlen(input); i++)    {         // Get the number of times the character        // is to be repeated        ascii = input[i];        repeat = ascii >= 97 ? ascii - 96 : ascii - 64;         for (int j = 0; j < repeat; j++)        {                         // if i is odd, print '!'            // else print '@'            if (i % 2 == 0)                cout << oddPos;            else                cout << evenPos;        }    }} // Driver codeint main(){    char input[100] = { 'A', 'b', 'C', 'd' };     // Encrypt the String    encrypt(input);    return 0;} // This code is contributed by// shubhamsingh10
// C program to Encrypt the String// using ! and @ #include <stdio.h>#include <string.h> // Function to encrypt the stringvoid encrypt(char input[100]){     // evenPos is for storing encrypting    // char at evenPosition    // oddPos is for storing encrypting    // char at oddPosition    char evenPos = '@', oddPos = '!';     int repeat, ascii;     for (int i = 0; i <= strlen(input); i++) {         // Get the number of times the character        // is to be repeated        ascii = input[i];        repeat = ascii >= 97 ? ascii - 96 : ascii - 64;         for (int j = 0; j < repeat; j++) {            // if i is odd, print '!'            // else print '@'            if (i % 2 == 0)                printf("%c", oddPos);            else                printf("%c", evenPos);        }    }} // Driver codevoid main(){    char input[100] = { 'A', 'b', 'C', 'd' };     // Encrypt the String    encrypt(input);}
// Java program to Encrypt the String// using ! and @class GFG{ // Function to encrypt the stringstatic void encrypt(char input[]){     // evenPos is for storing encrypting    // char at evenPosition    // oddPos is for storing encrypting    // char at oddPosition    char evenPos = '@', oddPos = '!';     int repeat, ascii;     for (int i = 0; i < input.length; i++)    {         // Get the number of times the character        // is to be repeated        ascii = input[i];        repeat = ascii >= 97 ?                  ascii - 96 : ascii - 64;         for (int j = 0; j < repeat; j++)        {            // if i is odd, print '!'            // else print '@'            if (i % 2 == 0)                System.out.printf("%c", oddPos);            else                System.out.printf("%c", evenPos);        }    }} // Driver codepublic static void main(String[] args){    char input[] = { 'A', 'b', 'C', 'd' };     // Encrypt the String    encrypt(input);}} // This code is contributed by PrinciRaj1992
# Python3 program to Encrypt the String# using ! and @ # Function to encrypt the stringdef encrypt(input_arr) :     # evenPos is for storing encrypting    # char at evenPosition    # oddPos is for storing encrypting    # char at oddPosition    evenPos = '@'; oddPos = '!';     for i in range(len(input_arr)) :         # Get the number of times the character        # is to be repeated        ascii = ord(input_arr[i]);        repeat = (ascii - 96 ) if ascii >= 97 \                               else (ascii - 64);         for j in range(repeat) :                         # if i is odd, print '!'            # else print '@'            if (i % 2 == 0) :                print(oddPos, end = "");            else :                print(evenPos, end = ""); # Driver codeif __name__ == "__main__" :     input_arr = [ 'A', 'b', 'C', 'd' ];     # Encrypt the String    encrypt(input_arr);     # This code is contributed by AnkitRai01
// C# program to Encrypt the String// using ! and @using System;using System.Collections.Generic; class GFG{ // Function to encrypt the stringstatic void encrypt(char []input){     // evenPos is for storing encrypting    // char at evenPosition    // oddPos is for storing encrypting    // char at oddPosition    char evenPos = '@', oddPos = '!';     int repeat, ascii;     for (int i = 0; i < input.Length; i++)    {         // Get the number of times the character        // is to be repeated        ascii = input[i];        repeat = ascii >= 97 ?                ascii - 96 : ascii - 64;         for (int j = 0; j < repeat; j++)        {            // if i is odd, print '!'            // else print '@'            if (i % 2 == 0)                Console.Write("{0}", oddPos);            else                Console.Write("{0}", evenPos);        }    }} // Driver codepublic static void Main(String[] args){    char []input = { 'A', 'b', 'C', 'd' };     // Encrypt the String    encrypt(input);}} // This code is contributed by 29AjayKumar
<script>     // JavaScript program to Encrypt the    // String using ! and @         // Function to encrypt the string    function encrypt(input)    {         // evenPos is for storing encrypting        // char at evenPosition        // oddPos is for storing encrypting        // char at oddPosition        let evenPos = '@', oddPos = '!';         let repeat, ascii;         for (let i = 0; i < input.length; i++)        {             // Get the number of times the character            // is to be repeated            ascii = input[i].charCodeAt();            repeat = ascii >= 97 ?                    ascii - 96 : ascii - 64;             for (let j = 0; j < repeat; j++)            {                // if i is odd, print '!'                // else print '@'                if (i % 2 == 0)                    document.write(oddPos);                else                    document.write(evenPos);            }        }    }         let input = [ 'A', 'b', 'C', 'd' ];       // Encrypt the String    encrypt(input); </script>
!@@!!!@@@@
 
princiraj1992
29AjayKumar
ankthon
gp6
SHUBHAMSINGH10
divyeshrabadiya07
encoding-decoding
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Constructors in C++
Operator Overloading in C++
Copy Constructor in C++
Polymorphism in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not