为什么要在arrayLetter中输出垃圾而不是char值?
当我把for循环放在方法调用上方打印arrayLetter时,它工作;当我把它放在方法调用下方时,它不工作我没有在方法上或主要上改变arrayLetter这是一个简单的程序,转移索引和输出转移的字母表。
#include <stdio.h>
#include <stdlib.h>
int* getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample);
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[]);
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample);
int main(void) {
char arrayLetter[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int key[] = { 21, 4, 2, 19, 14, 17};
char plainTextExample[] = { 'h', 'e', 'r', 'e',
'i', 's',
'h', 'o', 'w',
'i', 't',
'w', 'o', 'r', 'k', 's'};
int sizeArrayLetter = sizeof(arrayLetter)/sizeof(arrayLetter[0]);
int sizePlainTextExample = sizeof(plainTextExample)/sizeof(plainTextExample[0]);
int sizeKey = sizeof(key)/sizeof(key[0]);
int *arrayMatchAtIndex;
int *encryptArray;
char *cipherArray;
arrayMatchAtIndex = getMatchedIndex(arrayLetter, plainTextExample, sizeArrayLetter, sizePlainTextExample);
encryptArray = vigenereCipherEncryptIndex(key, sizeKey, sizeArrayLetter, sizePlainTextExample, arrayMatchAtIndex);
cipherArray = convertToNewEncryptedArray(arrayLetter, encryptArray, sizeArrayLetter, sizePlainTextExample);
for(char i=0; i < sizeArrayLetter; i++)
{
//printf("%s%i%s", "Indexes of Encryption: ", encryptArray[i], " \n");
printf("%s", arrayLetter);
}
free(arrayMatchAtIndex);
free(encryptArray);
free(cipherArray);
return EXIT_SUCCESS;
}
//compare arrays and store index into array
int *getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample)
{
int* arrayMatchAtIndex = malloc(sizePlainTextExample* sizeof(int));
//loop plainTextExample
for(int i=0; i < sizePlainTextExample ; i++)
{
//loop arrayLetter
for(int j=0; j < sizeArrayLetter ; j++)
{
//compare -> match then store arrayLetter index into arrayMatchAtIndex
if(plainTextExample[i] == arrayLetter[j])
{
arrayMatchAtIndex[i] = j;
}
}
}
return arrayMatchAtIndex;
}
//encrypt array by adding 'key' indexes into array
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[])
{
//make new array
int* encryptArray = malloc(sizePlainTextExample* sizeof(int));
int j=0;
//encrypt element -> (add elements of arrayIndexes and key) mod 26
for(int i=0; i < sizePlainTextExample; i++)
{
if(i >= sizeKey)
{
j = (i % sizeKey);
key[i] = key[j];
}
encryptArray[i] = (arrayMatchAtIndex[i]+key[i]) % 26;
// printf("%s%d%s%d%s", "Match Element: ", arrayMatchAtIndex[i], " key ", key[i], " ");
// printf("%s%d%s", "encryptElement: ", encryptArray[i], " \n");
}
return encryptArray;
}
//convert encrypt array index to its elements
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample)
{
char* cipherArray = malloc(sizePlainTextExample* sizeof(int));
//encrypted index - > convert elements to new index
for(int i=0; i < sizePlainTextExample ; i++)
{
for(int j=0; j< sizeArrayLetter; j++)
{
//find index->match then put Encrypted index into letter element
if(encryptArray[i] == j)
{
cipherArray[i] = arrayLetter[j];
printf("%c", arrayLetter[2]);
}
}
}
return cipherArray;
}
最佳答案
正如Jonathan Leffler清楚地说的那样,您的数组只是一个数组,而不是一个C字符串(以nul字符序列结尾)仅对C字符串使用%s
。
将循环更改为:
for (char i=0; i<sizeArrayLetter; i++) {
putchar(arrayletter[i]);
}
putchar('\n');
这将分别打印数组中的所有字符和额外的行尾。