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
// 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
filter_none
Java
// 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
filter_none
Python3
# 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
filter_none
C#
// 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
filter_none
Output:
!@@!!!@@@@
Recommended Posts:
- Encrypt the given string with the following operations
- Encrypt a string by repeating i-th character i times
- Encrypt a string into the Rovarspraket (The Robber Language)
- Encrypt string with product of number of vowels and consonants in substring of size k
- Program to remove vowels from a String
- C program to find the length of a string
- Program to print all substrings of a given string
- Program to extract words from a given String
- Program for Reversed String Pattern
- Program to duplicate Vowels in String
- Program to toggle all characters in a string
- Program for length of a string using recursion
- C++ Program to compare two string using pointers
- C++ Program to remove spaces from a string
- C Program to Check if a Given String is Palindrome
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.

