I am curious why this code compiles correctly but only returns the plaintext taken from the user. It does not encipher the plaintext even though the algorithm in the code states so.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
string message=get_string("Enter a message to encrypt: \n");
int key=get_int("Enter a key to encrypt the message: \n");
char m[strlen(message)];
strcpy(m, message);
for(int i=0; i<strlen(message); i++)
{
char letter=m[i];
if(isalpha(m[i])||isupper(m[i])||islower(m[i]))
{
if(letter>='a'&&letter<='z')
{
letter=letter+key;
if(letter>'z')
{
letter=letter-'z'+'a'-1;
letter=m[i];
}
letter=m[i];
}
else if(letter>='A'&&letter<='Z')
{
letter=letter+key;
if(letter>'Z')
{
letter=letter-'Z'+'A'-1;
letter=m[i];
}
letter=m[i];
}
}
printf("%c", m[i]);
}
printf("\n");
return 0;
}
m
is never changed after the plaintext is read into it. I suggest taking a closer look at the order of your assignments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.