Question: Create a program that encrypts and decrypts messages. The message should encrypt by rotating each alphabetical character by their position in the message. For example,

Create a program that encrypts and decrypts messages. The message should encrypt by "rotating" each alphabetical character by their position in the message. For example, the string "abc", should be encrypted to "bdf." Since the 'a' is the first character in the message, it is rotated 1 letter 'b'. The second letter, 'b', is rotated two positions to 'd' and 'c' the third letter is rotated three letters to 'f' So the word "zoo" is encrypted to "aqr" Notice that 'z' wrapped around to 'a'

This is what I have so far. Please try to only use #include

#include

int main ()

{

int i = 0;

char message[1000];

int ans;

char ch;

printf("Please enter your message:");

message[i] = getchar();

while (message[i] != ' ')

{

i = i +1;

message[i] = getchar();

}

message [i] = '\0';

printf(" %s",message);

printf("Would you like to (1) encrypt this message or (2) decrypt it : ");

scanf(" %i", &ans);

//encrypts message

if(ans == 1)

{

ch = message[i];

if( ch >= 'a' && ch <= 'z')

{

ch = ch + ans;

if (ch > 'z')

{

ch = ch - 'z' + 'a' -1;

}

message[i] = ch;

}

else if( ch >= 'A' && ch <= 'Z')

{

ch = ch + ans;

if (ch > 'Z')

{

ch = ch - 'Z' + 'A' -1;

}

message[i] = ch;

}

}

//decrypts message

if (ans == 2)

{

if (ch>= 'a' && ch <='z')

{

ch = ch - ans;

if (ch < 'a')

{

ch = ch + 'z' -'a' +1;

}

message[i] = ch;

}

else if( ch >= 'A' && ch <= 'Z')

{

ch = ch - ans;

if(ch < 'A')

{

ch = ch + 'Z' - 'A' + 1;

}

message[i] = ch;

}

}

printf("message is: %s ", message);

}

if(ans == 1)

{

ch = message[i];

if( ch >= 'a' && ch <= 'z')

{

ch = ch + ans;

if (ch > 'z')

{

ch = ch - 'z' + 'a' -1;

}

message[i] = ch;

}

else if( ch >= 'A' && ch <= 'Z')

{

ch = ch + ans;

if (ch > 'Z')

{

ch = ch - 'Z' + 'A' -1;

}

message[i] = ch;

}

}

if (ans == 2)

{

if (ch>= 'a' && ch <='z')

{

ch = ch - ans;

if (ch < 'a')

{

ch = ch + 'z' -'a' +1;

}

message[i] = ch;

}

else if( ch >= 'A' && ch <= 'Z')

{

ch = ch - ans;

if(ch < 'A')

{

ch = ch + 'Z' - 'A' + 1;

}

message[i] = ch;

}

}

printf("message is: %s ", message);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!