2017-09-09 106 views
-6

我一直在努力通过舒适版本的cs50的凯撒问题,并只想通过说我是一个完整的初学者来说这个问题。CS50凯撒 - ASCII字母和输出格式

该程序应该要求用户输入一个密钥和明文,然后使用提供的密钥对该文本进行密码处理。例如,对于2的密钥,“aba”将是“cdc”。

我想我设法让程序正常运行,除了两件事:密文或加密文本的输出应该是“Ciphertext:”的形式,而不是像我的程序那样只输出字符串。另一个更重要的问题是,如果输入的密钥大于26,输出包含其他不需要的符号而不仅仅是字母。

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

int main (int argc, string argv []) 
{ 
    if (argc != 2) 
    { 
     printf ("Error. Instructions not followed.\n"); 
     return 1; 
    } 
    int key = atoi(argv [1]); 
    printf ("%i\n", key); 
    printf("Plaintext:"); 
    string plaintext = get_string(); 
    int i = 0; 
    for (char c = 'A'; c <= 'Z'; c++) 
    { 
     i++; 
    } 
    for (int j = 0; j < strlen(plaintext); j++) 
    { 
     char ciphertext = ((plaintext[j]+key%26)); 
     if (isupper (plaintext[j]) || islower (plaintext[j])) 
     { 
      printf("%c", ciphertext); 
     } 
     if (isalpha (plaintext [j]) == false) 
     { 
      printf("%c", (plaintext[j])); 
     } 
    } 

printf("\n"); 
} 

任何帮助将不胜感激。谢谢。

+1

C没有字符串类型。什么是'字符串'? –

+0

第一个for循环没有做任何事情? –

+0

'“密文:”'??代码打印在哪里? –

回答

0

首先,你必须遵循问题的规范。也就是说,在输入任何输出之前,您必须打印出'纯文本'一词,然后输出'ciphertext:'。随着检查过程的自动化,这变得非常重要。注意你的问题陈述中的这些单词的情况。其次,我会让你自己弄清楚逻辑,这是问题集的重要组成部分。尝试观看下几个讲座,并学习如何在CS50 IDE中使用调试器。它会帮助你自己解决代码中的问题。