Program to Encrypt a String using ! and @
Last Updated :
27 Mar, 2023
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 <bits/stdc++.h>
using namespace std;
// 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)
cout << oddPos;
else
cout << evenPos;
}
}
}
// Driver code
int main()
{
char input[100] = { 'A', 'b', 'C', 'd' };
// Encrypt the String
encrypt(input);
return 0;
}
// This code is contributed by
// shubhamsingh10
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);
}
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
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
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
JavaScript
<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>
Time Complexity: O(n * 26), where n is the size of the given char array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2: Using strings and index() method
C++
// C++ program to encrypt a string using ! and @ symbols
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Input string to be encrypted
string str
= "CDE"; // Define lowercase and uppercase alphabets
string lower = "abcdefghijklmopqrstuvwxyz";
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Resultant string
string res = "";
// Iterate through the input string
for (int i = 0; i < str.length(); i++) {
// If the current index is even
if (i % 2 == 0) {
// If the current character is a lowercase
// letter
if (lower.find(str[i]) != string::npos) {
// Append '!' to the resultant string as
// many times as the position of the
// character in the lowercase alphabet
res += string(lower.find(str[i]) + 1, '!');
}
// If the current character is an uppercase
// letter
else {
// Append '!' to the resultant string as
// many times as the position of the
// character in the uppercase alphabet
res += string(upper.find(str[i]) + 1, '!');
}
}
// If the current index is odd
else {
// If the current character is a lowercase
// letter
if (lower.find(str[i]) != string::npos) {
// Append '@' to the resultant string as
// many times as the position of the
// character in the lowercase alphabet
res += string(lower.find(str[i]) + 1, '@');
}
// If the current character is an uppercase
// letter
else {
// Append '@' to the resultant string as
// many times as the position of the
// character in the uppercase alphabet
res += string(upper.find(str[i]) + 1, '@');
}
}
}
// Print the encrypted string
cout << res << endl;
return 0;
}
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
// Input string to be encrypted
String str = "CDE";
// Define lowercase and uppercase alphabets
String lower = "abcdefghijklmopqrstuvwxyz";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Resultant string
String res = "";
// Iterate through the input string
for (int i = 0; i < str.length(); i++) {
// If the current index is even
if (i % 2 == 0) {
// If the current character is a lowercase letter
if (lower.indexOf(str.charAt(i)) != -1) {
// Append '!' to the resultant string as many times as the position of the
// character in the lowercase alphabet
res += String.join("", Collections.nCopies(lower.indexOf(str.charAt(i)) + 1, "!"));
}
// If the current character is an uppercase letter
else {
// Append '!' to the resultant string as many times as the position of the
// character in the uppercase alphabet
res += String.join("", Collections.nCopies(upper.indexOf(str.charAt(i)) + 1, "!"));
}
}
// If the current index is odd
else {
// If the current character is a lowercase letter
if (lower.indexOf(str.charAt(i)) != -1) {
// Append '@' to the resultant string as many times as the position of the
// character in the lowercase alphabet
res += String.join("", Collections.nCopies(lower.indexOf(str.charAt(i)) + 1, "@"));
}
// If the current character is an uppercase letter
else {
// Append '@' to the resultant string as many times as the position of the
// character in the uppercase alphabet
res += String.join("", Collections.nCopies(upper.indexOf(str.charAt(i)) + 1, "@"));
}
}
}
// Print the encrypted string
System.out.println(res);
}
}
Python3
# Python3 program to Encrypt the String
# using ! and @
string = "CDE"
lower="abcdefghijklmopqrstuvwxyz"
upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res=""
for i in range(0,len(string)):
if(i%2==0):
if(string[i] in lower):
res+="!"*(lower.index(string[i])+1)
else:
res+="!"*(upper.index(string[i])+1)
else:
if(string[i] in lower):
res+="@"*(lower.index(string[i])+1)
else:
res+="@"*(upper.index(string[i])+1)
print(res)
C#
// C# program to encrypt a string using ! and @ symbols
using System;
class Gfg
{
static void Main(string[] args)
{
// Input string to be encrypted
string str = "CDE";
// Define lowercase and uppercase alphabets
string lower = "abcdefghijklmopqrstuvwxyz";
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Resultant string
string res = "";
// Iterate through the input string
for (int i = 0; i < str.Length; i++)
{
// If the current index is even
if (i % 2 == 0)
{
// If the current character is a lowercase letter
if (lower.Contains(str[i]))
{
// Append '!' to the resultant string as
// many times as the position of the
// character in the lowercase alphabet
res += new string('!', lower.IndexOf(str[i]) + 1);
}
// If the current character is an uppercase letter
else
{
// Append '!' to the resultant string as
// many times as the position of the
// character in the uppercase alphabet
res += new string('!', upper.IndexOf(str[i]) + 1);
}
}
// If the current index is odd
else
{
// If the current character is a lowercase letter
if (lower.Contains(str[i]))
{
// Append '@' to the resultant string as
// many times as the position of the
// character in the lowercase alphabet
res += new string('@', lower.IndexOf(str[i]) + 1);
}
// If the current character is an uppercase letter
else
{
// Append '@' to the resultant string as
// many times as the position of the
// character in the uppercase alphabet
res += new string('@', upper.IndexOf(str[i]) + 1);
}
}
}
// Print the encrypted string
Console.WriteLine(res);
}
}
JavaScript
// Javascript ptogram for the above approach
let string = "CDE";
let lower = "abcdefghijklmopqrstuvwxyz";
let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let res = "";
for (let i = 0; i < string.length; i++) {
if (i % 2 == 0) {
if (lower.includes(string[i])) {
res += "!".repeat(lower.indexOf(string[i]) + 1);
} else {
res += "!".repeat(upper.indexOf(string[i]) + 1);
}
} else {
if (lower.includes(string[i])) {
res += "@".repeat(lower.indexOf(string[i]) + 1);
} else {
res += "@".repeat(upper.indexOf(string[i]) + 1);
}
}
}
console.log(res);
// This code is contributed by codebraxnzt
Time Complexity: O(n * 26), where n is the length of the given string.
Auxiliary Space: O(n * 26)
Similar Reads
Decrypt the Message string by using Key Phrases Given two strings Encrypted message and a key phrase. The message is encrypted using a key phrase. Find the original message by decrypting the encrypted message using a key phrase by following the below-mentioned rules: The number of words in the message and key phrase will always be equal.The first
15 min read
Decrypt a string according to given rules Given encrypted string str, the task is to decrypt the given string when the encryption rules are as follows: Start with the first character of the original string.In every odd step, append the next character to it.In every even step, prepend the next character to the encrypted string so far.For exa
15+ min read
Encrypt the string - 2 Given a lowercase English string s of length n, encrypt it as follows:Replace each contiguous group of the same character with the character followed by the lowercase hexadecimal representation of its length.Reverse the entire modified string.Return the encrypted string.Note: All Hexadecimal letters
7 min read
Program to find the Encrypted word Given a string, the given string is an encrypted word, the task is to decrypt the given string to get the original word. Examples: Input: str = "abcd"Output: bdeeExplanation:a -> a + 1 -> bb -> b + 2 -> dc -> c + 2 -> ed -> d + 1 -> e Input: str = "xyz"Output: yaaExplanation:
4 min read
How to encrypt passwords in a Spring Boot project using Jasypt In this article, we will learn how to encrypt data in Spring Boot application config files like application.properties or application.yml. Inside those files, we can encrypt username, password, etc. You often come across developing projects where you have to connect to databases like MongoDB, etc, a
4 min read
Encrypt a string by repeating i-th character i times Given string str, the task is to encrypt the string with the given encryption algorithm. The 1st character of the string will be repeated once in the encrypted string, the 2nd character will be repeated twice, â¦, nth character will be repeated n times. Examples: Input: str = "geeks" Output: geeeeekk
4 min read
Javascript Program To Write Your Own atoi() The atoi() function in C takes a string (which represents an integer) as an argument and returns its value of type int. So basically the function is used to convert a string argument to an integer. Syntax: int atoi(const char strn) Parameters: The function accepts one parameter strn which refers to
5 min read
Strong Password Suggester Program Given a password entered by the user, check its strength and suggest some password if it is not strong. Criteria for strong password is as follows : A password is strong if it has : At least 8 characters At least one special char At least one number At least one upper and one lower case char. Exampl
15 min read
Output of Java Programs | Set 52 (Strings Class) Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program Java class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new
5 min read
Add index to characters and reverse the string Given a string str, the task is to encrypt and reverse the string. The string is encrypted by adding every character of the string with it's index in the string i.e. if character'a' is at index 2 then the character in the updated string will be 'a' + 2 = 'c'. Since value of string may go beyond 256,
4 min read