<分区>
Possible Duplicate:
Sizeof an array in the C programming language?
我一直在摆弄 C 以更好地熟悉它,并且认为我可能偶然发现了一个我不确定如何解决的初始化/指针问题。下面的程序是 ROT13 的一个实现,所以它接受一个输入字符串,并将每个字母移动 13,从而得到密文。我的程序的输出显示了正确的类次,但它不能用于超过 4 个字符,这让我怀疑 sizeof 是否使用不正确。感谢任何其他建议,我敢肯定我现在搞砸了一些事情。
#include <stdio.h>
#include <string.h>
void encrypt(char *);
int main(void){
char input[] = "fascs";
encrypt(input);
return 0;
}
void encrypt(char *input){
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
printf("Input: %s \n", input);
int inputCount = sizeof(input);
printf("Characters in Input: %i \n\n", inputCount);
//holds encrypted text
char encryptedOutput[inputCount];
//Initialize counters
int i, j = 0;
// loop through alphabet array, if input=current letter, shift 13 mod(26),
// push result to output array, encryptedOutput
for(i = 0; i < inputCount; i++){
for(j = 0; j < 26; j++){
if(input[i] == alphabet[j]){
encryptedOutput[i] = alphabet[(j + 13) % 26];
}
}
}
//Nul Termination for printing purposes
encryptedOutput[i] = '\0';
printf("Rot 13: %s \n\n", encryptedOutput);
}