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

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!