(C Program Only, Please not C++) Using Characters and Strings. Write a simple pr
ID: 3823067 • Letter: #
Question
(C Program Only, Please not C++)
Using Characters and Strings.
Write a simple program to encrypt a text string using a look-up table or simple algorithm. For instance, the text string “Hello World” could be encrypted into “lfmmp Xpsme” simply by translating each character into the next character in the alphabet. Make up your own encryption scheme or table and test it with a program to input the user’s text string and display it in encrypted form.
Then write a companion program to decrypt the text string encrypted in the first program. You could test it by having the user enter the encrypted output from the first program, and the decryption program should display the original text string.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Maximum name size + 1. */
#define MAX_NAME_SZ 50
char* encryted(char inputString[]){
int i = 0;
while(inputString[i] != ''){
if(inputString[i] != ' '){
inputString[i]=inputString[i]+3;
}
i++;
}
return inputString;
}
void decryted(char inputString[]){
int i = 0;
while(inputString[i] != ''){
if(inputString[i] != ' '){
inputString[i]=inputString[i]-3;
}
i++;
}
}
void main(){
char *string = malloc (MAX_NAME_SZ);
char *tmp = NULL;
int spcnt[50],i,j,c;
printf("Enter a string : ");
fgets (string, MAX_NAME_SZ, stdin);
printf("%s",string);
//encode
tmp = encryted(string);
printf(" ");
printf("Encoded string is as follow : %s",string);
//decode
decryted(tmp);
printf(" ");
printf("De-Encoded string is as follow : %s ",string);
}
Output
-------------
sh-4.2$ main
Enter a string : Hello world
Hello world
Encoded string is as follow : Khoor zruog
De-Encoded string is as follow : Hello world
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.