我从学校获得了一项任务,可以制作一个加密和解密文本的程序。我必须使用这个声明:
int encrypted(char *plainText, int arrLength, int key, char *cipherText);
目前,当我在main.c中使用for循环(我在myfunctions.c中显示的那个)时,我可以使caesar密码工作,但是当我在另一个文件中写入for循环时(myfunctions.c) )使用上面的声明,它编译并运行,但似乎myfunctions.c中的for循环不会像它应该执行。
这是我的main.c:
#include <stdio.h>
#include <string.h>
#include "myfunctions.h"
int main(){
int key, arrLength, menu=0;
char plainText[100], cipherText[100], result[100];
printf("Encrypt\n");
printf("Enter your key (1-25): ");
scanf("%d", &key);
printf("Write the word or sentece you want to encrypt: ");
fgets(plainText, 100, stdin);
arrLength=strlen(plainText);
encrypted(plainText, arrLength, key, result);
getchar();
return 0;
}
myfunctions.c:
#include "myfunctions.h"
#include <stdio.h>
#include <string.h>
int encrypted(char *plainText, int arrLength, int key, char *cipherText){
int result = 0;
for(int i = 0; i < arrLength; i++)
{
// encryption
result = (*plainText + key);
// wrapping after Z for uppercase letters
if (isupper(*plainText) && (result > 'Z'))
{
result = (result - 26);
}
// wrapping after z for lowercase letters
if (islower(*plainText) && (result > 'z'))
{
result = (result - 26);
}
if (isalpha(*plainText))
{
printf("%c", result);
}
else
{
printf("%c", *plainText);
}
}
return 1;
}
myfunctions.h
#ifndef myfunctions_h
#define myfunctions_h
int encrypted(char *plainText, int arrLength, int key, char *cipherText);
#endif
答案 0 :(得分:4)
plainText
的for循环中包含encrypted()
。fgets()
在纯文本之前阅读换行符。试试这个main
功能
int main(){
int key, arrLength, menu=0;
char keyText[100],plainText[100], cipherText[100], result[100];
printf("Encrypt\n");
printf("Enter your key (1-25): ");
fgets(keyText, 100, stdin);
sscanf(keyText, "%d", &key);
printf("Write the word or sentece you want to encrypt: ");
fgets(plainText, 100, stdin);
arrLength=strlen(plainText);
encrypted(plainText, arrLength, key, result);
return 0;
}
并更改循环for(int i = 0; i < arrLength; i++)
到for(int i = 0; i < arrLength; i++, plainText++)