la rints the number of occurrences of each vowel to stdout. Exercise 5.9. Write
ID: 3820918 • Letter: L
Question
la rints the number of occurrences of each vowel to stdout. Exercise 5.9. Write a program, called hide, according to the following spec ification: (1) It has two possible arguments: -encrypt indicates encryption mode, while -decrypt indicates decryption mode. 2 It reads an arbitrary list of strings from stdin. It applies a cypher to the strings. You may invent your own, but a simple one is to shift the letters by a constant amount (for example, 'a' becomes 'd', and 'z' becomes c'). It either encrypts or decrypts the strings ("shifts" or "deshifts" the letters) depending on the mode. (4) It prints the encrypted or decrypted text to stdout. At minimum, it should be able to handle text consisting only of lowercase letters. For example, if the message attention home planet stop prepare invasion stop earth is ripe for the taking stop cu soon full stop is in file msg.txt, then ./hide encrypt msg.txt msge.txt would produce the following cyphertext in file msge.txt if hide is using a shift of 12: mffqzfuaz tayq bxmzaf efab bdqbmdq uzhmeuaz efab qmdft ue dubq rad ft fmwuzs efab og eaaz rgxx efabExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
char* encryptMessage(char* message,int key);
char* decryptMessage(char* encMessage ,int key);
int main( int argc, char *argv[] ) {
char *message,*emessage,*dmessage;
int i,key;
if( argc == 2 ) {
printf("The argument supplied is %s ", argv[1]);
clrscr();
printf(" Enter the shift key ");
scanf("%d",&key);
key=key%26;
printf(" Enter message ");
fflush(stdin);
gets(message);
for(i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);
if (strcmp(argv[1],"encrypt") == 0 ){
emessage = encryptMessage(message,key);
}
else if(strcmp(argv[1],"encrypt") == 0 ){
dmessage = decryptMessage(message,key);
}
}
else {
printf("Invalid no of arguments ");
}
getch();
return(0);
}
char* encryptMessage(char* message,int key){
{
char* emessage;
int j = 0,temp
for(i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);
for(i=0;message[i]!=NULL;i++)
{
if(message[i]==' ')
emessage[j++]=message[i];
else
{
if(message[i]>=48 && message[i]<=57)
{
temp=message[i]+key;
if(temp>57)
emessage[j++]=48+(temp-58);
else
emessage[j++]=temp;
}
else
{
if(message[i]>=97 && message[i]<=123)
{
temp=message[i]+key;
if(temp>122)
emessage[j++]=97+(temp-123);
else
emessage[j++]=temp;
}
else
emessage[j++]=message[i];
}
}
}
emessage[j]='';
printf(" Encrypted message is ");
for(i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
return eMessage;
}
char* decryptMessage(char* emessage,int key){
{
char* dmessage;
int j = 0,temp;
for(i=0,j=0;emessage[i]!=NULL;i++)
{
if(emessage[i]==' ')
dmessage[j++]=emessage[i];
else
{
if(emessage[i]>=48 && emessage[i]<=57)
{
temp=emessage[i]-key;
if(temp<48)
dmessage[j++]=58-(48-temp);
else
dmessage[j++]=temp;
}
else
{
if(emessage[i]>=97 && emessage[i]<=123)
{
temp=emessage[i]-key;
if(temp<97)
dmessage[j++]=123-(97-temp);
else
dmessage[j++]=temp;
}
else
dmessage[j++]=emessage[i];
}
}
}
dmessage[j]='';
printf(" Retrieved message is ");
for(i=0;dmessage[i]!=NULL;i++)
printf("%c",dmessage[i]);
return decryptMessage;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.