Program to Encrypt a String using ! and @

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to Encrypt the String
// using ! and @
  
#include <stdio.h>
#include <string.h>
  
// Function to encrypt the string
void 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 code
void main()
{
    char input[100] = { 'A', 'b', 'C', 'd' };
  
    // Encrypt the String
    encrypt(input);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Encrypt the String
// using ! and @
class GFG 
{
  
// Function to encrypt the string
static 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 code
public static void main(String[] args)
{
    char input[] = { 'A', 'b', 'C', 'd' };
  
    // Encrypt the String
    encrypt(input);
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Encrypt the String 
# using ! and @ 
  
# Function to encrypt the string 
def 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 code 
if __name__ == "__main__"
  
    input_arr = [ 'A', 'b', 'C', 'd' ]; 
  
    # Encrypt the String 
    encrypt(input_arr); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Encrypt the String
// using ! and @
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function to encrypt the string
static 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 code
public static void Main(String[] args)
{
    char []input = { 'A', 'b', 'C', 'd' };
  
    // Encrypt the String
    encrypt(input);
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

!@@!!!@@@@


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.