vendredi 29 mai 2015

fgets() creating another copy of string at time of input?

I'm wondering what the error is here? I'm making a causer cipher, and it works fine but when I print the encrypted message at the end, it reprints the unencrypted message. When I comment out and set message to a sentence, it works fine. But when I get a string using fgets it creates another copy. Thanks in advance!

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>

int main(int argc, char *argv[]) 
{
    char message[100];  
    printf("Please Enter a message to be Encrypted: ");
    fgets(message, 100, stdin);
    unsigned char caeser[] = "";
    int j = 0;
    if(argc >= 2)
    {
    int k = atoi(argv[1]);
    for(int i = 0, n = strlen(message); i < n; i++)
    {
        printf("%i\n",n);
        if(!isspace(message[i]))
        {
            if(islower(message[i]))
            {
                j = message[i] - 97;
                caeser[i] = 97 + ((j + k) % 26);
            }
            if(isupper(message[i]))
            {
                j = message[i] - 65;
                caeser[i] = 65 + ((j + k) % 26);
            }
        }   
        else
        {
            caeser[i] = ' ';
        }
       }
    printf("%s\n", caeser);
    }
}

Aucun commentaire:

Enregistrer un commentaire