Create a program that encrypts and decrypts messages. The message should encrypt
ID: 3724425 • Letter: C
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, 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 <stdio.h>
#include <stdio.h>
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] = '';
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);
}
Explanation / Answer
The program is modified and i am providing correct program
#include <stdio.h>
#include<string.h>
int main ()
{
int i = 0;
char message[1000];
int ans;
unsigned char ch;
printf("Please enter your message:");
message[i] = getchar();
while (message[i] != ' ')
{
i = i +1;
message[i] = getchar();
}
message [i] = '';
i=0;
//encrypts message
while(i<strlen(message))
{
ch = message[i];
if( ch >= 97 && ch <=122)
{
ch = ch + (i+1);
if(ch>122)
{
ch = (ch - 'z'-1) + 'a';
}
}
else if( ch >= 65 && ch <=90)
{
ch = ch + (i+1);
if(ch>90)
{
ch = (ch - 'Z'-1) + 'A';
}
}
message[i]=ch;
++i;
}
printf("message after encryption is: %s ", message);
i=0;
//decrypt message
while(i<strlen(message))
{
ch = message[i];
if( ch >=97 && ch <=122)
{
ch = ch - (i+1);
if(ch<97)
{
ch ='z'-(97-ch-1);
}
}
else if( ch >=65 && ch <=96)
{
ch = ch - (i+1);
if(ch<65)
{
ch ='z'-(65-ch-1);
}
}
message[i]=ch;
++i;
}
printf("message after decryption is: %s ", message);
}
Output
Please enter your message:abcxyz
message after encryption is: bdfbdf
message after decryption is: abcxyz
...Program finished with exit code 0
Press ENTER to exit console.
2)
Please enter your message:abc
message after encryption is: bdf
message after decryption is: abc
...Program finished with exit code 0
Press ENTER to exit console.
3)
Please enter your message:zoo
message after encryption is: aqr
message after decryption is: zoo
...Program finished with exit code 0
Press ENTER to exit console.
4)
Please enter your message:pqrstuvwxyz
message after encryption is: qsuwyacegik
message after decryption is: pqrstuvwxyz
...Program finished with exit code 0
Press ENTER to exit console.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.