[英]Vigenere's Cipher CS50 if loops
As part of pset2 CS50, for vigerne's cipher.作为 pset2 CS50 的一部分,用于 vigerne 的密码。 My question is how should I arrange my loops of 'if's?
我的问题是我应该如何安排我的“if”循环? I don't think i did them exactly right.
我不认为我做的完全正确。
And pls point out any errors/bad things in my code as well.并且请指出我的代码中的任何错误/坏事。 (my indentation seems bad)
(我的缩进看起来很糟糕)
So far, the encryption I got matches the one they provided on cs50.到目前为止,我得到的加密与他们在 cs50 上提供的加密相匹配。 But im looking to improve this.
但我希望改善这一点。 Thanks in advance.
提前致谢。
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc ,string argv[])
{
// Make sure 1 argument is given
if(argc !=2)
{
printf("Please enter only 1 alphabetical word as key\n");
return 1; //returns an error if no 2nd arg is provided.
}
// Converting 2nd argument into an integer
string k=argv[1];
for(int i=0,n=strlen(k);i<n;i++)
{
if(!isalpha(k[i]))
{
char c= k[i];
printf("%c is not an alphabet. Please enter only alphabets.\n",c);
return 2; //returns an error if key has non-alphabets
}
}
// Prompt for plaintext to be encrypted
printf("Please enter the plain text you wish to encrypt\n");
string p=GetString();
printf("This is your text: %s\n",p);
// Converting upper/lower case alphabets to 0-25 indexed
for(int i=0,j=0,n=strlen(p),m=strlen(k);i < n; i++,j++)
{
if(p[i]==' ')
{
j--;
}
if(isupper(k[j]))
{
k[j] = (int)(k[j]-'A');
}
if(islower(k[j]))
{
k[j] = (int)(k[j]-'a');
}
// printf("%d\n",k[i]); // value of k[i] is still stored as int here for ea char
if(isalpha(p[i]))
{
if(isupper(p[i]))
{
int newpindex =(p[i]-'A');
int x = (newpindex + k[(j+m)%(m)])%26;
int cipher = x + 'A';
printf("%c",cipher);
}
else if(islower(p[i]))
{
int newpindex =(p[i]-'a');
int x = (newpindex + k[(j+m)%(m)])%26;
int cipher = x + 'a';
printf("%c",cipher);
}
}
else
printf("%c",p[i]);
}
printf(" \n");
}
One possible error in the code is the identification of characters that are neither letter nor spaces.代码中一种可能的错误是识别出既不是字母也不是空格的字符。
Looking at your code, j increments on every iteration of the loop, requiring it to decrement when a space is encountered.查看您的代码, j 在循环的每次迭代中递增,要求它在遇到空格时递减。 However, what if the non-alphabetic character is an exclamation point or other symbol?
但是,如果非字母字符是感叹号或其他符号怎么办?
Consider if there would be a better way to handle j within the loop to make sure you don't lose your place in the key in such situations.考虑是否有更好的方法来处理循环中的 j 以确保在这种情况下不会失去在键中的位置。 This can also eliminate your need to check to check for spaces (or symbols in general), thereby making the code a little more concise.
这也可以消除您检查空格(或一般符号)的需要,从而使代码更加简洁。
Edit: Just saw something else, isupper and islower encompass isalpha (the manual page for isalpha details that it is the equivalent of isupper || is lower).编辑:刚看到别的东西,isupper 和 islower 包含 isalpha(isalpha 详细信息的手册页,它相当于 isupper || 更低)。 So you have the option of removing the isalpha condition and only checking for isupper/islower.
因此,您可以选择删除 isalpha 条件并仅检查 isupper/islower。
if(p[i]==' ')
{
j--;
}
This if statement wouldn't work considering the first character in the string k is a space since that would make j=-1 so using that in the next if statement as p[-1] wouldn't work.考虑到字符串 k 中的第一个字符是一个空格,这个 if 语句不起作用,因为这会使 j=-1 所以在下一个 if 语句中使用它作为 p[-1] 不起作用。 A better option would be removing this if statement so in a situation when p[i] is a space, it is displayed just as it is with no modifications.
一个更好的选择是删除这个 if 语句,这样在 p[i] 是一个空格的情况下,它会按原样显示,没有修改。
To make your code look cleaner you could remove the curly brackets from the if statements that only have one instruction.为了使您的代码看起来更简洁,您可以从只有一条指令的 if 语句中删除大括号。
For example:例如:
if(isupper(k[j]))
k[j] = (int)(k[j]-'A');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.