[英]C Vigenere Cipher Not Printing
I've been tasked with creating a vigenere cipher, but my program is not printing out anything. 我的任务是创建vigenere密码,但是我的程序没有输出任何内容。 Thing is, I'm not sure where the problem is;
问题是,我不确定问题出在哪里。 is the file not reading in, is my logic incorrect, etc.?
文件没有读入,我的逻辑不正确等等吗? Any help as to where I messed is appreciated.
任何帮助我弄乱的地方都表示赞赏。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void encrypt(char *theString, int shift)
{
if (isalpha(*theString) && isupper(*theString))
{
*theString += shift;
if (*theString >= 'Z')
{
*theString = *theString - 26;
}
}
theString++;
}
int main(void)
{
FILE *inputFile;
char KEY[256];
char theString[80];
int shift;
int i;
inputFile = fopen("code.txt", "r");
if (inputFile == NULL)
{
printf("Failed to open\n");
return(0);
}
fgets(theString, sizeof(theString), stdin);
printf("Enter the Key: ");
fgets(KEY, sizeof(KEY), stdin);
for (i = 0; i < 256; i++)
{
shift = KEY[i] - 65;
encrypt(theString,shift);
puts(theString);
}
return(0);
}
The reason you're not seeing any output is because this happens first: 您没有看到任何输出的原因是因为这首先发生:
fgets(theString, sizeof(theString), stdin);
That reads a string from standard input, and waits until you press Enter . 它将从标准输入中读取一个字符串,然后等待您按Enter 。 So it looks like the program is stuck.
因此,该程序似乎卡住了。 You should print a prompt first, such as:
您应该首先打印一个提示,例如:
printf("Enter a string: ");
Your encrypt
loop only modifies the first character of the input string. 您的
encrypt
循环仅修改输入字符串的第一个字符。 You need a loop to modify every character: 您需要一个循环来修改每个字符:
void encrypt(char *theString, int shift)
{
for ( ; *theString != '\0'; theString++)
{
if (isupper(*theString))
{
*theString += shift;
if (*theString >= 'Z')
{
*theString = *theString - 26;
}
}
}
}
Other points: 其他要点:
isupper()
implies isalpha()
; isupper()
暗示isalpha()
; no need for both fgets()
returns NULL
on error; fgets()
在错误时返回NULL
; you should check for it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.