📜  使用 ! 加密字符串的程序和 @

📅  最后修改于: 2022-05-13 01:57:07.990000             🧑  作者: Mango

使用 ! 加密字符串的程序和 @

给定一个字符串,任务是使用!加密这个字符串和@符号,或者。在加密消息时,加密格式必须按照字母顺序重复符号与字母位置一样多的次数。
例子:

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

方法:这是一种非常基本和简单的加密技术,可以按如下方式完成:

  • 从String中一一获取字符
  • 对于每个字符,获取该字符的 ASCII 值与“A”(如果字符是大写字母)或“a”(如果字母是小写字母)之间的差异。这将是重复加密字符的次数。
  • 对于字符串的第 i 个字符,如果 i 为奇数,则加密字符为 '!'如果我是偶数,加密字符将是'@'。

下面是上述代码的实现:

C++
// C++ program to Encrypt the String
// using ! and @
#include 
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 
#include 
 
// 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


输出:
!@@!!!@@@@