首页 > 其他分享 >pset2 substitution.c

pset2 substitution.c

时间:2024-12-04 21:34:20浏览次数:9  
标签:using pset2 int substitution length key printf new

1.extension:To Do Tasks

  1. 推荐一个vscode里面一个很好用的插件!!!写出解决的步骤,不但理清楚思路。还可以提高效率!
  2. 特别是针对一些文本比较长的pset,要求多且零碎,反复切换页面(浏览器到vscode)就有点太累了,而且截图固定的话,整个页面也看着不舒服,太乱了,所以这个插件可以节省时间。
  3. 在写出解决的步骤(或者你不确定也没有关系),有一些必要的步骤可以先开始试试看,一步一步的,每次做完@done都有一个时间,你会有慢慢的有成就感,所以说,把可以解决的先解决,最后自然就出来了。
  4. 其实写的时间不长,但是中间小错误需要调试的点很多,有时候都不知道在哪,所以说不要太盯着结果了,耐心的去找过程,这个时候找到错误的速度就会出奇的快!Unbelievable!好像有点理解过程比结果更重要了><

在这里插入图片描述
使用方法:首先的话,是先创建new.todo文件,然后就是一些快捷键,如下图,还是比较好上手哒!
在这里插入图片描述pset2例子:
在这里插入图片描述

2 code

#include <cs50.h>   //for get_sting
#include <ctype.h>  //for tolower
#include <stdio.h>  //for printf
#include <stdlib.h> //for malloc
#include <string.h> //for strlen
int main(int argc, string argv[])
{
    // 1 command-line argument
    if (argc != 2)
    {
        printf("./substitution key\n");
        return 1;
    }
    /*2 check if valid key
    get the user input:argv[1]
        * contain 26 digits of characters purely :isapha()
        * no matter is uppercase or lowercase
        * A -Z:65-90 a-z:97-122
        * a letter in the key can only show once:frequency(every letter) == 1
        * if not ,printf message and return 1
    */
    size_t length = strlen(argv[1]);
    if (length != 26)
    {
        printf("Key need to contain 26 letters!\n");
        return 1;
    }
    int *frequency = (int *) calloc(length, sizeof(int));
    for (int i = 0; i < length; i++)
    {
        if (!isalpha(argv[1][i]))
        {
            printf("Key must be purely alphabetically!\n");
            return 1;
        }
        argv[1][i] = tolower(argv[1][i]);
        char ch = argv[1][i];
        frequency[(int) ch - 97]++;
        // printf("count: %d\n", frequency[(int)ch - 97]);

        if (frequency[(int) ch - 97] != 1)
        {
            printf("The letters in key can not repeated!\n");
            return 1;
        }
    }
    // store the key
    string key = malloc(length + 1);
    if (key == NULL)
    {
        printf("failed to allocate memory\n");
        return 1;
    }
    for (int k = 0; k < length; k++)
    {
        key[k] = argv[1][k];
    }
    // 3 get the plaintext
    string s = get_string("Plaintext: ");
    // printf("plaintext: %s\n", s);
    /*4 output text
         * calculate the length of s
         * allocate new memory
         * convert new[]:This 26-character key means that
           A (the first letter of the alphabet)
           should be converted into N (the first character of the key)...
           key:NQXPOMAFTRHLZGECYJIUWSKDVB key[position] = 'H'-> position
           alp:ABCDEFGHIJKLMNOPQRSTUVWXYZ
           example:HELLO --> FOLLE new[i] = key[int(s[i]) - 97]
   tolower-alpha: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
               -97
           index:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   */
    size_t s_length = strlen(s);
    string new = malloc(strlen(s) + 1);
    if (new == NULL)
    {
        printf("failed to allocate memory");
        return 1;
    }
    printf("ciphertext:");
    for (int j = 0; j < s_length; j++)
    {
        // keep the format
        if (isupper(s[j]))
        {
            new[j] = toupper(key[s[j] - 65]);
        }
        else if (islower(s[j]))
        {
            new[j] = tolower(key[s[j] - 97]);
        }
        else
        {
            new[j] = s[j];
        }
        printf("%c", new[j]);
    }
    printf("\n");

    return 0;
}

3 check:

:) substitution.c exists
:) substitution.c compiles
:) encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
:) encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
:) encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
:) encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
:) does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
:) handles lack of key
:) handles too many arguments
:) handles invalid key length
:) handles invalid characters in key
:) handles duplicate characters in uppercase key
:) handles duplicate characters in lowercase key
:) handles multiple duplicate characters in key

标签:using,pset2,int,substitution,length,key,printf,new
From: https://blog.csdn.net/typingok7/article/details/144249746

相关文章