gpt4 book ai didi

c - Sizeof 返回不正确的数组长度

转载 作者:太空宇宙 更新时间:2023-11-04 05:07:38 24 4
gpt4 key购买 nike

<分区>

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);

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com