0% found this document useful (0 votes)
25 views2 pages

Data Enc&Dec

Uploaded by

Vaishnavi Peddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Data Enc&Dec

Uploaded by

Vaishnavi Peddi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <string.h>
#include <ctype.h>

void encrypt(char text[], char result[]) {


for (int i = 0; text[i] != '\0'; i++) {
if (isupper(text[i])) {
result[i] = (text[i] - 'A' + 3) % 26 + 'A'; // Encrypt Uppercase
} else if (islower(text[i])) {
result[i] = (text[i] - 'a' + 3) % 26 + 'a'; // Encrypt Lowercase
} else {
result[i] = text[i]; // Keep other characters unchanged
}
}
result[strlen(text)] = '\0'; // Null-terminate the result
}

void decrypt(char text[], char result[]) {


for (int i = 0; text[i] != '\0'; i++) {
if (isupper(text[i])) {
result[i] = (text[i] - 'A' - 3 + 26) % 26 + 'A'; // Decrypt Uppercase
} else if (islower(text[i])) {
result[i] = (text[i] - 'a' - 3 + 26) % 26 + 'a'; // Decrypt Lowercase
} else {
result[i] = text[i]; // Keep other characters unchanged
}
}
result[strlen(text)] = '\0'; // Null-terminate the result
}

int main() {
char text[100], result[100];
int choice;

printf("Enter the text: ");


fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0'; // Remove newline character

printf("Choose an option:\n");
printf("1. Encrypt the text\n");
printf("2. Decrypt the text\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);

switch (choice) {
case 1:
encrypt(text, result);
printf("Encrypted text: %s\n", result);
break;
case 2:
decrypt(text, result);
printf("Decrypted text: %s\n", result);
break;
default:
printf("Invalid choice. Please select 1 or 2.\n");
}

return 0;
}

You might also like