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++ 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 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 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 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# 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 |
<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++ 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;
} |
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 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# 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 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)