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++
#include <bits/stdc++.h>
using namespace std;
void encrypt( char input[100])
{
char evenPos = '@' , oddPos = '!' ;
int repeat, ascii;
for ( int i = 0; i <= strlen (input); i++)
{
ascii = input[i];
repeat = ascii >= 97 ? ascii - 96 : ascii - 64;
for ( int j = 0; j < repeat; j++)
{
if (i % 2 == 0)
cout << oddPos;
else
cout << evenPos;
}
}
}
int main()
{
char input[100] = { 'A' , 'b' , 'C' , 'd' };
encrypt(input);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void encrypt( char input[100])
{
char evenPos = '@' , oddPos = '!' ;
int repeat, ascii;
for ( int i = 0; i <= strlen (input); i++) {
ascii = input[i];
repeat = ascii >= 97 ? ascii - 96 : ascii - 64;
for ( int j = 0; j < repeat; j++) {
if (i % 2 == 0)
printf ( "%c" , oddPos);
else
printf ( "%c" , evenPos);
}
}
}
void main()
{
char input[100] = { 'A' , 'b' , 'C' , 'd' };
encrypt(input);
}
|
Java
class GFG
{
static void encrypt( char input[])
{
char evenPos = '@' , oddPos = '!' ;
int repeat, ascii;
for ( int i = 0 ; i < input.length; i++)
{
ascii = input[i];
repeat = ascii >= 97 ?
ascii - 96 : ascii - 64 ;
for ( int j = 0 ; j < repeat; j++)
{
if (i % 2 == 0 )
System.out.printf( "%c" , oddPos);
else
System.out.printf( "%c" , evenPos);
}
}
}
public static void main(String[] args)
{
char input[] = { 'A' , 'b' , 'C' , 'd' };
encrypt(input);
}
}
|
Python3
def encrypt(input_arr) :
evenPos = '@' ; oddPos = '!' ;
for i in range ( len (input_arr)) :
ascii = ord (input_arr[i]);
repeat = (ascii - 96 ) if ascii > = 97 \
else (ascii - 64 );
for j in range (repeat) :
if (i % 2 = = 0 ) :
print (oddPos, end = "");
else :
print (evenPos, end = "");
if __name__ = = "__main__" :
input_arr = [ 'A' , 'b' , 'C' , 'd' ];
encrypt(input_arr);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void encrypt( char []input)
{
char evenPos = '@' , oddPos = '!' ;
int repeat, ascii;
for ( int i = 0; i < input.Length; i++)
{
ascii = input[i];
repeat = ascii >= 97 ?
ascii - 96 : ascii - 64;
for ( int j = 0; j < repeat; j++)
{
if (i % 2 == 0)
Console.Write( "{0}" , oddPos);
else
Console.Write( "{0}" , evenPos);
}
}
}
public static void Main(String[] args)
{
char []input = { 'A' , 'b' , 'C' , 'd' };
encrypt(input);
}
}
|
Javascript
<script>
function encrypt(input)
{
let evenPos = '@' , oddPos = '!' ;
let repeat, ascii;
for (let i = 0; i < input.length; i++)
{
ascii = input[i].charCodeAt();
repeat = ascii >= 97 ?
ascii - 96 : ascii - 64;
for (let j = 0; j < repeat; j++)
{
if (i % 2 == 0)
document.write(oddPos);
else
document.write(evenPos);
}
}
}
let input = [ 'A' , 'b' , 'C' , 'd' ];
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++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str
= "CDE" ;
string lower = "abcdefghijklmopqrstuvwxyz" ;
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
string res = "" ;
for ( int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
if (lower.find(str[i]) != string::npos) {
res += string(lower.find(str[i]) + 1, '!' );
}
else {
res += string(upper.find(str[i]) + 1, '!' );
}
}
else {
if (lower.find(str[i]) != string::npos) {
res += string(lower.find(str[i]) + 1, '@' );
}
else {
res += string(upper.find(str[i]) + 1, '@' );
}
}
}
cout << res << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
String str = "CDE" ;
String lower = "abcdefghijklmopqrstuvwxyz" ;
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
String res = "" ;
for ( int i = 0 ; i < str.length(); i++) {
if (i % 2 == 0 ) {
if (lower.indexOf(str.charAt(i)) != - 1 ) {
res += String.join( "" , Collections.nCopies(lower.indexOf(str.charAt(i)) + 1 , "!" ));
}
else {
res += String.join( "" , Collections.nCopies(upper.indexOf(str.charAt(i)) + 1 , "!" ));
}
}
else {
if (lower.indexOf(str.charAt(i)) != - 1 ) {
res += String.join( "" , Collections.nCopies(lower.indexOf(str.charAt(i)) + 1 , "@" ));
}
else {
res += String.join( "" , Collections.nCopies(upper.indexOf(str.charAt(i)) + 1 , "@" ));
}
}
}
System.out.println(res);
}
}
|
Python3
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#
using System;
class Gfg
{
static void Main( string [] args)
{
string str = "CDE" ;
string lower = "abcdefghijklmopqrstuvwxyz" ;
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
string res = "" ;
for ( int i = 0; i < str.Length; i++)
{
if (i % 2 == 0)
{
if (lower.Contains(str[i]))
{
res += new string ( '!' , lower.IndexOf(str[i]) + 1);
}
else
{
res += new string ( '!' , upper.IndexOf(str[i]) + 1);
}
}
else
{
if (lower.Contains(str[i]))
{
res += new string ( '@' , lower.IndexOf(str[i]) + 1);
}
else
{
res += new string ( '@' , upper.IndexOf(str[i]) + 1);
}
}
}
Console.WriteLine(res);
}
}
|
Javascript
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);
|
Time Complexity: O(n * 26), where n is the length of the given string.
Auxiliary Space: O(n * 26)
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...