我正在做 CS50 的凯撒问题集,当我尝试移动大写字母时,使用 if (isupper(argument) == true)
来检查我想移动的字符是否为大写,它没有用,它认为大写字母是' t,实际上是大写。当我将它切换到 if (isupper(argument))
时,程序正确地转换了大写字母。
这两种格式有什么区别吗?
这是我使用的代码(我指的是 for 循环中的代码):
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc,char *argv[])
{
//Check wether there is only 1 command line argument
if (argc == 2)
{
//Check if there is any character that's not a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (isdigit(argv[1][i]) == false)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
//Convert key to an int
int key = atoi(argv[1]);
//Prompt plaintext
string plaintext = get_string("plaintext: ");
string ciphertext = plaintext;
//Shift ciphertext's characters by the amount of "key"
for (int i = 0; i < strlen(plaintext); i++)
{
//If it isn't a letter,do nothing
if (isalpha(plaintext[i]) == false)
{
ciphertext[i] = plaintext[i];
}
else
{
//If it's uppercase
if (isupper(plaintext[i]) == true)
{
//Convert ASCII to alphabetical index
plaintext[i] -= 'A';
//Shift alphabetical index
ciphertext[i] = (plaintext[i] + key) % 26;
//Convert alphabetical index to ASCII
ciphertext[i] += 'A';
}
//If it's lowercase
else if (islower(plaintext[i]))
{
//Convert ASCII to alphabetical index
plaintext[i] -= 'a';
//Shift alphabetical index
ciphertext[i] = (plaintext[i] + key) % 26;
//Convert alphabetical index to ASCII
ciphertext[i] += 'a';
}
}
}
//Print ciphertext
printf("ciphertext: %s\n",ciphertext);
}