Question
Printing it to encrypt and decrypt a sentence or a single word. This is what I have so far but every time I encrypt a
Printing it to encrypt and decrypt a sentence or a single word. This is what I have so far but every time I encrypt a sentence, special characters appear. How to fix?
#include
int main ()
{
int i = 0;
char message[1000];
int ans;
// char ch;
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
i = 0;
if( ans == 1)
{
while( message[i] != '\0')
{
ch = message[i];
if( ch >= 'a' && ch <= 'z')
{
ch = ch + (i+1);
if (ch > 'z')
{
ch = (ch - 'z' -1)+ 'a';
}
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch +(i +1);
if (ch >'Z')
{
ch = (ch - 'Z' - 1) + 'A';
}
}
message[i] = ch;
i = i+1;
}
printf("Your Encrypted message is: %s ", message);
}
// Decrypts the message
if( ans == 2)
{
while(message[i] != '\0')
{
ch = message[i];
if (ch>= 'a' && ch <='z')
{
ch = ch -(i+1);
if (ch < 'a')
{
ch = 'z' - ('a' - ch -1);
}
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch - (i+1);
if(ch < 'A')
{
ch = 'Z' - ('A'- ch- 1);
}
}
message[i] = ch;
i = i+1;
}
printf(" decrypted message is: %s ", message);
}
message[i] = '\0';
i = i+1;
//printf("The is: %s ", message);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started