c - 文本加密仅适用于小写,大写字母不变
问题描述
我正在处理 pset,我需要一些帮助。该程序的目的是获取命令行参数并将其用作密钥来加密用户输入的一些明文,并将其移动给定的字母数。大写字母应保持大写,小写 - 小写。所有其他符号应保持不变。我的代码只加密小写字母而不加密大写字母。请帮我找出原因。
void encryption(string plaintext, int k, string ciphertext);
int main(int argc, string argv[])
{
string plaintext;
int n = strlen(plaintext);
char ciphertext[n+1]; //the lenght of the plaintext+ 1 extra char which i
int k = atoi(argv[1]); //Convert string into an integer, i.e parsing
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
for (int i = 0, m = strlen(argv[1]); i < m; i++)
{
if (isdigit(argv[1][i]))
{
plaintext = get_string("Plaintext:");
encryption(plaintext, k, ciphertext); //calling the encryption function
printf("Ciphertext: %s\n", ciphertext);
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
}
void encryption(string plaintext, int k, string ciphertext)
{
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
if (isupper(plaintext[i]))
{
int pi = plaintext[i] - 65;
char ci = ((pi + k) % 26) + 65;
ciphertext[i] = ci;
}
if (islower(plaintext[i]))
{
int pi1 = plaintext[i] - 97;
char ci1 = ((pi1 + k) % 26) + 97;
ciphertext[i] = ci1;
}
else
{
ciphertext[i] = plaintext[i];
}
}
}
解决方案
推荐阅读
- javascript - 将事件侦听器附加到类方法
- android - Kotlin 在延迟后启用滑动刷新
- php - 一个数组中没有重复的 id 和 post_title 的 SQL 结果数组
- google-bigquery - SIMBA ODBC 驱动程序
- c++ - 使用编译程序
:对“gvContext”的未定义引用 - angular - 在 app.component.ts 中将 Observable 添加到 ngOnInit 后,Reactive Form 停止工作
- python-3.x - 为什么树未定义但已经导入 tkinter & tkinter.ttk python
- c++ - 将输入重定向到线程
- python - 根据矩阵二进制值选择列表值?
- docker - Docker swarm:无法从同一部署的容器内部访问公共服务