使用一些字符串操作和只

时间:2022-12-28 18:26:46

I am trying to use only one library and I encountered a few issues. This program should do the following :

我只尝试使用一个库,但遇到了一些问题。本程序应做到以下几点:

  • it should have an encryption function and another one for decryption.
  • 它应该有一个加密功能和一个解密功能。
  • string must be 8 capped letters or digs (A-Z 0-9)
  • 字符串必须为8个带帽字母或digs (A-Z 0-9)
  • the process should be like this :

    这个过程应该是这样的:

    • A : moving the given string 2 characters backwards
      before : abcdefg
      after : cdefgab

      A:将给定的字符串2字符向后移动:abcdefg后:cdefgab

    • B:Reverse the result of A Function.
      before : cdefgab
      after : bagfedc

      B:反转函数的结果。前:cdefgab后:bagfedc

    • C: Swap pairs characters with unpaired characters.
      before : ereaeedf
      after : reaeeefd

      C:交换成对的字符和未配对的字符。之前:ereaeedf after: reaeeefd

    • D: Going through an encryption table.
      two arrays [Original] and [Encrypted]
      take the string and divide it to chars each char run through the table and take his original position (A = 1 , B = 2 , C =3 and so on) and going to [Encrypted] array and take the character that is in the same position .

      通过一个加密表。两个数组(原始)和(加密)识字课每一个字符的字符串,并将其贯穿桌子上,把他的原始位置(A = 1,B = 2,C = 3等等),将(加密)数组和字符是在相同的位置。

      array[Original] = {"A","B","C","D","D","F");
      array[Encrypted]= {"%","a","h","k","c","^");
      

      before : FACD
      after : ^%hk

      后:FACD:^ %港元

My problem now is with Enc_Table which doesn't work as expected. I have no idea why. I have tried a few things nothing sorted it out.

我现在的问题是Enc_Table,它不像预期的那样工作。我不知道为什么。我已经尝试了一些东西,但没有办法解决。

My Code Source :

我的代码来源:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define charsize 8

bool isStringEncrypted(char[]);
void Encryption();
char* Rotate_R(char str[]);
char* Reverse(char str[]);
char* Swap(char str[]);
char* Enc_Table(char str[]);
int getPos(const char *array, size_t size, char c);

void main() {

    int choose = 0;
    while (choose != 3){
        printf("Menu: \n\n  Please choose an option: \n\n 1 - Encrypt \n 2 - Decrypt [Not Implemented Yet] \n 3 - Exit \n\nYour selection ? :");
        scanf("%d", &choose);
        switch (choose)                                
        {

            case 1:
                Encryption();
                break;

            case  2:
                //Decryption();
                break;

            case 3:
                break;

            default:
                printf("Wrong choice, try again!\n");

        }

    }
}


void Encryption(){

    char str[charsize];
    char result[charsize];
    do {
        printf("please enter the string with 8 characters\n =>");
        scanf("%s", str);
        printf("\n Variable Str Value  = %s \n", str);
    } while (isStringEncrypted(str) == false);


    printf("Swap = %s \n", Swap(str));
    printf("Rotate_R =  %s \n", Rotate_R(str));
    printf("Reverse = %s \n", Reverse(str));
    printf("Enc_Table = %s \n", Enc_Table(str));
    printf("\nEncrypted String : %s \n", Enc_Table(Reverse(Rotate_R(Swap(str)))));
    scanf("%s", result);
}


bool isStringEncrypted(char str[])
{
    int i = 0;
    int Letters = 0;
    int Numbers = 0;


    unsigned int len = 0;
    while (*(str + len)) len++;

    if (len != charsize)
    {
        printf(" \n %d = len != %d = Size \n", len, charsize);
        return false;
    }


    while (str[i] != NULL && i < charsize) {
        if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= '0' && str[i] <= '9') {

            if (str[i] >= 'A' && str[i] <= 'Z')
                Letters++;
            if (str[i] >= '0' && str[i] <= '9')
                Numbers++;
        }

        i++;
    }

    if (i == charsize && (Letters >= 2) && (Numbers >= 2))
        return true;
    else return true;
}

char* Rotate_R(char str[])
{
    char x, y;
    int i;
    x = str[7];
    y = str[6];

    for (i = 5; i>-1; i--)
    {
        str[i + 2] = str[i];
    }
    str[0] = y;
    str[1] = x;

    return str;
}

char* Swap(char str[])
{
    int i;
    char Temp;

    for (i = 0; i < charsize; i += 2)
    {
        Temp = str[i];
        str[i] = str[i + 1];
        str[i + 1] = Temp;
    }

    return str;
}

char* Reverse(char str[])
{
    unsigned int lens = 0;
    while (*(str + lens)) lens++;
    char temp;
    size_t len = lens - 1;
    size_t i;
    size_t k = len;

    for (i = 0; i < len; i++)
    {
        temp = str[k];
        str[k] = str[i];
        str[i] = temp;
        k--;
        if (k == (len / 2))
        {
            break;
        }
    }

    return str;
}

char* Enc_Table(char str[])
{

    int i;
    unsigned int lens = 0;
    while (*(str + lens)) lens++;

    char dec[38] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0' };
    char enc[38] = { 'S', '5', 'P', 'W', 'A', 'R', '8', 'Q', 'I', 'V', 'B', '0', 'D', 'Z', '3', 'C', '2', 'H', '4', '1', 'K', '7', 'G', 'Y', '6', 'O', 'J', 'E', 'U', 'N', 'F', '9', 'M', 'L', 'T', 'X', '\0' };

    for (i = 0; i < charsize; i++)
    {
        int index = getPos(dec, sizeof(dec), str[i]);
        str[i] = enc[index];
        i++;
    }

    return str;
}

int getPos(const char *array, size_t size, char c)
{
    for (size_t i = 0; i < size; i++)
    {
        if (array[i] == c)
            return (int)i;
    }
    return -1;
}

1 个解决方案

#1


2  

Encryption table should work like this:

加密表应该这样工作:

void table(char *str, int forward)
{
    const char dec[36] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char enc[36] = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    for (int i = 0, len = strlen(str); i < len; i++)
    {
        for (int n = 0; n < 36; n++)
        {
            if (str[i] == from[n])
            {
                str[i] = to[n];
                break;
            }
        }
    }
}

int main()
{
    char s[100];
    strcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

    table(s, 1);// encryption
    printf("%s\n", s);
    table(s, 0);// decryption
    printf("%s\n", s);
    return 0;
}

Also the same function with strcspn:

strcspn也有相同的函数:

void table(char *str, int forward)
{
    const char *dec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char *enc = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    char buf[2] = { 0 };
    for (int i = 0, len = strlen(str); i < len; i++)
    {
        buf[0] = str[i];
        int pos = strcspn(from, buf);
        if (pos < 36)
            str[i] = to[pos];
    }
}

While we are at it, a fast rotate function:

当我们在它的时候,一个快速旋转函数:

void rotate(char *str, int rotate, int direction /*1=rotate-right, 0=rotate-left*/)
{
    int length = strlen(str);
    if (!rotate || rotate >= length)
        return;

    int offset = direction ? (length - rotate) : rotate;

    char *left  = _strdup(str);
    char *right = _strdup(str + offset);

    //swap left and right to finish rotate 
    strcpy(str, right);
    strncat(str, left, offset);

    free(left);
    free(right);
}

#1


2  

Encryption table should work like this:

加密表应该这样工作:

void table(char *str, int forward)
{
    const char dec[36] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char enc[36] = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    for (int i = 0, len = strlen(str); i < len; i++)
    {
        for (int n = 0; n < 36; n++)
        {
            if (str[i] == from[n])
            {
                str[i] = to[n];
                break;
            }
        }
    }
}

int main()
{
    char s[100];
    strcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

    table(s, 1);// encryption
    printf("%s\n", s);
    table(s, 0);// decryption
    printf("%s\n", s);
    return 0;
}

Also the same function with strcspn:

strcspn也有相同的函数:

void table(char *str, int forward)
{
    const char *dec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char *enc = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    char buf[2] = { 0 };
    for (int i = 0, len = strlen(str); i < len; i++)
    {
        buf[0] = str[i];
        int pos = strcspn(from, buf);
        if (pos < 36)
            str[i] = to[pos];
    }
}

While we are at it, a fast rotate function:

当我们在它的时候,一个快速旋转函数:

void rotate(char *str, int rotate, int direction /*1=rotate-right, 0=rotate-left*/)
{
    int length = strlen(str);
    if (!rotate || rotate >= length)
        return;

    int offset = direction ? (length - rotate) : rotate;

    char *left  = _strdup(str);
    char *right = _strdup(str + offset);

    //swap left and right to finish rotate 
    strcpy(str, right);
    strncat(str, left, offset);

    free(left);
    free(right);
}