c - 维吉尼亚如何停止增量

标签 c increment cs50 vigenere

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>



int main(int argc, string argv[])
{
//string for chiper text
        char s[100];

//exit if no argument or more than one    
        if( argc != 2 ) 
       {
          printf("One argument expected.");
        return 1;
       }
//check the key string, exit if it's not alphabetic   
    for (int i = 0; i < argc; i++)
    {
        for (int j = 0, n = strlen(argv[1]); j < n; j++)
        {
            if (isalpha(argv[1][j]))
            {
            }
           else
           {
           printf("Key from a command-line-argument should be an 
                   alphabetic");
           return 1;
           }
        }

    }
//this will keep key
       int k;
//command line argument as a key string      
       string key = argv[1]; 

            printf("plaintext:  ");

            string p = get_string();   //plain text, string

            printf("chiper text: ");



            for (int i = 0, n=strlen(p); i < n; i++)    //iterate while 
                                                         plaintext ends
            {

                       for (int j = 0, x=i%strlen(key);  j <= x && 
                       isalpha(p[i]); j++ )  //iterate for leight of key 
                                            string and if p[i] alphabetic
                    {
                        if(islower(key[j]))
                        {
                        k=(int)key[j]-97;   
                        }
                    }

                            if (isalpha(p[i]))
                            {

                           //encripts capital letters
                                                     if (isupper(p[i])) 
                                                    {
                                                        s[i] = p[i]+k;
                                                        if (s[i] > 90)
                                                        {
                                                            s[i] = s[i]-26;
                                                        }
                                                    }

                                    //encripts lower case letter
                                                    if  (islower(p[i]))
                                                    {
                                                        s[i] = p[i]+k;
                                                        if (p[i]+k > 122)
                                                        {
                                                            s[i] = 
                                                        (p[i]+k)-26;
                                                        }
                                                     }   
                            }  
                            //don't encript non alphabetic character
                            else
                            {
                               s[i] = p[i];
                            }   
            printf("%c", s[i]);
            }
         return 0;   
}                                                                                

尝试解决问题 set2。凯撒很容易,缩写-小菜一碟。但是 Vigenere - 哦,这让我发疯! 如果没有字母,我找不到不增加 key 的方法。 J 一次又一次地迭代。我尝试了很多方法,现在却陷入困境。请帮忙。

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

typedef char* string; 


int main(int argc, string argv[]) {
    if (argc != 2) {
        printf("how to run: file_name keyword\n");
        return 1;
    }

    string key = argv[1];
    int k, key_length = strlen(key);
    for (k = 0; k < key_length; k++) {
        key[k] = tolower(key[k]) - 97; 
    }   

    printf("insert to encrypt:\n");
    char text[100];
    scanf("%[^\n]", text);
    printf("before: %s\n",text);

    int i, text_length = strlen(text);
    int ascii_before, alpha_before, alpha_after, ascii_after;
    for ( i= 0; i < text_length; i++) {
        if (isalpha(text[i])) {
            if (islower(text[i])) {//lower case
                ascii_before = text[i];
                alpha_before = ascii_before - 'a';
                //caesar formula
                alpha_after = (alpha_before + key[i%key_length]) % 26;
                ascii_after = alpha_after + 'a';
                text[i] = ascii_after;          
            } else { //upper case
                ascii_before = text[i];
                alpha_before = ascii_before - 'A';
                //caesar formula
                alpha_after = (alpha_before + key[i%key_length]) % 26;
                ascii_after = alpha_after + 'A';    
                text[i] = ascii_after;
            }
        }       
    }   

    printf("after: %s\n",text);
    return 0;
}

关于c - 维吉尼亚如何停止增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686473/

相关文章:

c - c 中的内存是如何分配的,为什么两个连续的内存之间的差异总是4?

c - 告诉客户端何时完成写入缓冲区

c - 为什么这些括号在C语言中给出不同的答案?

c - 即使使用 -gdwarf-2,GCC 4.8 也会在编译单元 header 中插入版本 4

scala - Scala 中的自增 (++) 运算符

php - MySQL 增量并避免不正确的引用

javascript - 迭代 boolean 值的二维数组并返回基于 true/false 的递增值

c - 仅在 C 中验证字母输入

c - 我应该使用 "rand % N"还是 "rand()/(RAND_MAX/N + 1)"?

C 字符串解析和转换