c - 根据密钥长度打印列中的值
问题描述
我有以下代码,并希望它根据我的密钥长度在列中打印出 plainText 和 cipherText 。
例如,在下面的示例中,密钥长度为 7,因此我希望它以 7 列的形式打印出结果。
int main(){
char* text = "The quick brown fox jumps over the lazy dog";
char* key = "pangram";
char* cipherText = Encipher(text, key, '-');
char* plainText = Decipher(cipherText, key);
printf("\nKey = %s",key );
printf("\nPlain text = %s",plainText);
printf("\nEncipher = %s",cipherText);
printf("\n");
}
所以结果可能看起来像这样,但是对于The quick brown fox jumps over the lazy dog
:
a t t a c k p
o s t p o n e
d u n t i l t
w o a m x y z
我的完整代码可在此处获得:https ://repl.it/repls/ShoddyPiercingDemos 。
解决方案
您可以使用strlen()
来计算长度,key
然后cipherText
使用简单的循环来打印值。
把它放在你的最后main()
看看结果:
size_t keylen = strlen(key);
size_t cipherLen = strlen(cipherText);
for (size_t i = 0; i < cipherLen; i++) {
if (i % (keylen - 1) == 0)
printf("%c\n", cipherText[i]);
printf("%c ", cipherText[i]);
}
输出:
h k m r a -
- u o o e d
d - b o s t
t y - i w j v
v o - e f
f p z - T c
c n u e l g q
q r x h -
-