Programa para encriptar una string usando ! y @

Dada una string, la tarea es cifrar esta string usando ! y los símbolos @ , alternativamente. Al cifrar el mensaje, el formato cifrado debe repetir el símbolo tantas veces como la posición de la letra en orden alfabético.
Ejemplos: 
 

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: !!!@@@@!!!!!

Enfoque: Este es un tipo de técnica de cifrado muy básico y simple y se puede realizar de la siguiente manera: 
 

  • Obtener el carácter uno por uno de la String
  • Para cada carácter, obtenga la diferencia entre el valor ASCII de ese carácter y ‘A’ (si el carácter es una letra mayúscula) o ‘a’ (si la letra es una letra minúscula). Este será el número de veces que se repetirá el carácter de cifrado.
  • Para el i-ésimo carácter de la string, si i es impar, el carácter de cifrado será ‘!’ y si i es par, el carácter de cifrado será ‘@’.

A continuación se muestra la implementación del código anterior: 
 

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>
Producción: 

!@@!!!@@@@

 

Publicación traducida automáticamente

Artículo escrito por Riyaz_Khan_Pathan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *