-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_decrypt.c
56 lines (50 loc) · 1.57 KB
/
encrypt_decrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <string.h>
#include "encrypt.h"
#include "decrypt.h"
int main()
{
char response[10]; // Allocate space for a single string
printf("\nWould you like to encrypt (E), decrypt (D), or finish (F)?\n");
while (scanf("%9s", response) == 1) // Read up to 9 characters to avoid overflow
{
// Clear the newline character left in the buffer
int c;
while ((c = getchar()) != '\n' && c != EOF);
if (strcmp(response, "E") == 0)
{
char msg[80];
printf("Enter your message: ");
if (fgets(msg, sizeof(msg), stdin))
{
// Remove newline character from fgets if present
size_t len = strlen(msg);
if (len > 0 && msg[len - 1] == '\n')
msg[len - 1] = '\0';
encrypt(msg);
printf("This is the encrypted message:%s\n", msg);
}
}
else if (strcmp(response, "D") == 0)
{
char word[80];
printf("Enter the word you want to decrypt: ");
if (scanf("%79s", word) == 1)
{
decrypt(word);
printf("Your decrypted message is: %s\n", word);
}
}
else if (strcmp(response, "F") == 0)
{
printf("Goodbye!\n");
return 0;
}
else
{
printf("Not a valid response, please try again:\n");
}
printf("\nWould you like to encrypt (E), decrypt (D), or finish (F)?\n");
}
return 0;
}