#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void encrypting(char cipher[25], int shift, int num)
{
int i;
for (i = 0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] + shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] + shift - 'a' + 26) % 26) + 'a');
}
}
}
void decrypting(char cipher[25], int shift, int num)
{
inti;
for (i = 0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] - shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] - shift - 'a' + 26) % 26) + 'a');
}
}
}
int main()
{
char text[10];
static const char encrypt[] = "2";
static const char decrypt[] = "1";
int shift;
char cipher[25];
int result1;
int result2;
int num;
int i;
printf("Enter operation: encrypt or decrypt/n");
printf("Press 1 to Encrypt or 2 to Decrypt");
scanf("%c", &text);
printf("Enter shift key");
scanf("%d", &shift);
printf("Enter text to encrypt/decrypt");
fflush(stdin);
scanf("%c", &cipher);
num = strlen(cipher);
result1 = strcmp(text, encrypt);
result2 = strcmp(text, decrypt);
if (result1 == 0)
{
decrypting(cipher, shift, num);
}
else { exit(0); }
if (result2 == 0)
{
encrypting(cipher, shift, num);
}
else { exit(0); }
printf("Result");
printf("%d", cipher);
}
程序在用戶輸入密碼文本後意外終止。我不知道現在的問題是什麼。任何人都可以解釋什麼是我的代碼現在的問題?所有的幫助表示讚賞。C程序意外終止
請系統縮進您的代碼並避免將縮進字符作爲縮進字符,至少對於發佈在SO上的代碼。 –