做char';C中的s是否预先指定了零索引值?

做char';C中的s是否预先指定了零索引值?,c,cs50,C,Cs50,很抱歉,如果我的标题有点误导,我对很多这方面还是新手,但是: 我最近参与了一个小的密码项目,用户可以在命令行给文件一个参数,但必须按字母顺序。(例如:./文件abc) 然后,此参数将用于公式中,对您提供的纯文本消息进行加密。多亏了我朋友的帮助,我的代码开始工作了,但我并不是这个公式的100%特定部分 #include <stdio.h> #include <cs50.h> #include <stdlib.h> #include <string.

很抱歉,如果我的标题有点误导,我对很多这方面还是新手,但是:

我最近参与了一个小的密码项目,用户可以在命令行给文件一个参数,但必须按字母顺序。(例如:./文件abc)

然后,此参数将用于公式中,对您提供的纯文本消息进行加密。多亏了我朋友的帮助,我的代码开始工作了,但我并不是这个公式的100%特定部分

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


int main (int argc, string argv[])
{   //Clarify that the argument count is not larger than 2
    if (argc != 2)
    {
        printf("Please Submit a Valid Argument.\n");
        return 1;
    }
    //Store the given arguemnt (our key) inside a string var 'k' and check if it is alpha
    string k = (argv[1]);
    //Store how long the key is
    int kLen = strlen(k);
    //Tell the user we are checking their key
    printf("Checking key validation...\n");
    //Pause the program for 2 seconds
    sleep(2);
    //Check to make sure the key submitted is alphabetical
    for (int h = 0, strlk = strlen(k); h < strlk; h++)
    {
        if isalpha(k[h])
        {
            printf("Character %c is valid\n", k[h]);
            sleep(1);
        }
        else
        {   //Telling the user the key is invalid and returning them to the console
            printf("Key is not alphabetical, please try again!\n");
            return 0;
        }

    }
    //Store the users soon to be enciphered text in a string var 'pt'
    string pt = get_string("Please enter the text to be enciphered: ");
    //A prompt that the encrypted text will display on
    printf("Printing encrypted text: ");
    sleep(2);
    //Encipher Function
    for(int i = 0, j = 0, strl = strlen(pt); i < strl; i++)
    {
        //Get the letter 'key'
        int lk = tolower(k[j % kLen]) - 'a';
        //If the char is uppercase, run the V formula and increment j by 1
        if isupper(pt[i])
        {
            printf("%c", 'A' + (pt[i] - 'A' + lk) % 26);
            j++;
        }
        //If the char is lowercase, run the V formula and increment j by 1
        else if islower(pt[i])
        {
            printf("%c", 'a' + (pt[i] - 'a' + lk) % 26);
            j++;
        }
        //If the char is a symbol just print said symbol
        else
        {
            printf("%c", pt[i]);
        }
    }
    printf("\n");
    printf("Closing Script...\n");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符串argv[])
{//请澄清参数计数不大于2
如果(argc!=2)
{
printf(“请提交一个有效的参数。\n”);
返回1;
}
//将给定的arguemnt(我们的键)存储在字符串var“k”中,并检查它是否为alpha
字符串k=(argv[1]);
//存储钥匙的长度
int-kLen=strlen(k);
//告诉用户我们正在检查他们的密钥
printf(“检查密钥验证…\n”);
//暂停程序2秒钟
睡眠(2);
//检查以确保提交的密钥按字母顺序排列
for(inth=0,strlk=strlen(k);h
加密功能:
将“A”用作占位符的字符,但“A”是否自动保留零索引值?(B=1,C=2,…)

在C中,像
'A'
这样的字符文字属于
int
类型,代表系统上编码字符A的任何整数值。在99.999…%使用ASCII字符编码的系统上,这是数字65。如果您有一台20世纪70年代使用EBCDIC的旧IBM大型机,那么它可能是另一种东西。您会注意到,代码正在减去
'A'
,以生成基于0的值


这确实假设字母A-Z占据26个连续代码。ASCII码(A=65、B=66等)也是如此,但并非所有代码都是如此,而且该语言也不保证这一点。

在C语言中,像
'A'
这样的字符文字属于
int类型,代表系统中编码字符A的任何整数值。在99.999…%使用ASCII字符编码的系统上,这是数字65。如果您有一台20世纪70年代使用EBCDIC的旧IBM大型机,那么它可能是另一种东西。您会注意到,代码正在减去
'A'
,以生成基于0的值

这确实假设字母A-Z占据26个连续代码。ASCII码(A=65、B=66等)也是如此,但并非所有代码都是如此,语言也不保证这一点

“A”是否自动保留零索引值?(B=1,C=2,…)

不可以。严格一致的C代码不能依赖于除连续表示数字
0
-
9
以外的任何字符编码,即使公共字符集确实连续表示它们

关于字符集的唯一保证是:

。。。上述十进制数字列表中0后的每个字符的值应比前一个字符的值大一个

字符集(例如)不连续表示字母

“A”是否自动保留零索引值?(B=1,C=2,…)

不可以。严格一致的C代码不能依赖于除连续表示数字
0
-
9
以外的任何字符编码,即使公共字符集确实连续表示它们

关于字符集的唯一保证是:

。。。上述十进制数字列表中0后的每个字符的值应比前一个字符的值大一个


字符集(如不连续表示字母)的顺序性只是关注的一个方面

正确使用isalpha(ch)
是另一个问题,在OP的代码中没有很好地实现

isalpha(ch)
期望
ch
unsigned char
EOF
范围内。对于
k[h]
,一个
char
,该值可能为负值。使用以下各项确保非负值:

// if  isalpha(k[h])
if isalpha((unsigned char) k[h])

顺序性只是关注的一个方面

正确使用isalpha(ch)
是另一个问题,在OP的代码中没有很好地实现

isalpha(ch)
期望
ch
unsigned char
EOF
范围内。对于
k[h]
,一个
char
,该值可能为负值。使用以下各项确保非负值:

// if  isalpha(k[h])
if isalpha((unsigned char) k[h])

char
是一种数字类型,通常也用于表示可见字符(或特殊的不可见字符)