简体   繁体   English

如何将char保存到数组中ascii num的位置

[英]How to save a char to an array in the position of its ascii num In c

I am working on a Caesar cipher and I am trying to save the characters of my cipher key in an array based on the ascii of the letter is supposed to represent. 我正在研究Caesar密码,并且正在尝试将我的密码密钥的字符保存在基于字母所代表的字母的数组中。 So if my cipher char key[] = "codezyxwvutsrqpnmlkjihgfba" The first char in the array (c) is supposed to represent the letter a, whose ascii num is 97. So I want to store c in the array in the 97th spot. 因此,如果我的密码char key [] =“ codezyxwvutsrqpnmlkjihgfba”数组(c)中的第一个字符应该表示字母a,其ascii num为97。所以我想将c存储在数组中的第97个位置。 Everytime I try to this the array turns out empty. 每次我尝试这样做时,数组都会变成空的。

char key[] = {"codezyxwvutsrqpnmlkjihgfba"};

char alphabet[] = {"abcdefghijklmnopqrstuvwxyz"};

char answerKey[200] = "";

for (int i = 0; key[i] != '\0'; i++) {
    answerKey[(int) alphabet[i]] = key[i];
}

for (int i = 0; answerKey[i] != '\0'; i++) {
    printf("%c", answerKey[i]);
} 

Since the answerkey array has values only in the range of 97 - 122 assuming you are only using lower alphabet the other elements of the arrays are garbage. 由于假设只使用小写字母,应答键数组的值仅在97-122范围内,因此数组的其他元素是垃圾。

Just change the print for loop to iterate from 97 to 122 and you get what you want. 只需将print for循环更改为从97迭代到122,即可获得所需的内容。

char key[] = {"codezyxwvutsrqpnmlkjihgfba"};

char alphabet[] = {"abcdefghijklmnopqrstuvwxyz"};

char answerKey[200]="";

for (int i = 0; key[i] != '\0'; i++) {
    printf("%c",alphabet[i]);
    answerKey[(int) alphabet[i]] = key[i];
    printf("%c",answerKey[(int)alphabet[i]]);
}
printf("\n");
int i=0;
for (i = 97;i<=122; i++) 
{
    printf("%c", answerKey[i]);
} 

You are starting your printing of the answerKey[] array at the first element and telling it to stop as soon as it hits '\\0' . 您将从第一个元素开始打印answerKey[]数组,并告诉它在达到'\\0'立即停止。 I don't believe answerKey[0] should ever not be '\\0' since none of the printable ascii characters are 0. I would expect your answerKey[] array to be empty except between elements 97-122, so if your cipher will be used only for lowercase alphabetical characters, perhaps only look in that part of the array. 我不认为answerKey[0]永远不应该为'\\0'因为所有可打印的ascii字符都不为0。我希望您的answerKey[]数组为空,除了元素97-122之间,因此,如果密码会仅用于小写字母字符,也许只在数组的该部分中查找。

Alternatively, you could make your answerKey[] array only hold enough space to fit your cipher by subtracting 'a' from the element address as you're placing it. 另外,您可以通过在放置元素地址时从元素地址中减去'a' ,使answerKey[]数组仅容纳足够的空间以适合密码。 Something like this might do the trick: 这样的事情可能会解决问题:

char answerKey[27] = "";

for (int i = 0; key[i] !='\0'; i++) {
    answerKey[(int) alphabet[i] - 'a'] = key[i];
}

In C, you can convert a char to an int by simply preforming a cast. 在C中,您可以通过简单地执行强制转换将char转换为int。

In the memory, when you have a char 'a', the value 97 is saved. 在内存中,当您有一个字符“ a”时,将保存值97。 When you use a char, it is just the way you understand what is written in the memory. 使用char时,这只是了解内存中写入内容的方式。 you can just treat this memory as an int, and get the value which is stored over there. 您可以将该内存视为一个int,然后获取存储在该内存中的值。

For example: 例如:

char c = 'a';
printf("char is %c, int is %d", c, (int)(c));
// Output would be:
//   char is a, int is 97

For further information, read: How are different types stored in memory 有关更多信息,请阅读: 内存中如何存储不同类型的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM