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


i'm wondering error here? i'm making causer cipher, , works fine when print encrypted message @ end, reprints unencrypted message. when comment out , set message sentence, works fine. when string using fgets creates copy. 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 message encrypted: ");     fgets(message, 100, stdin);     unsigned char caeser[] = "";     int j = 0;     if(argc >= 2)     {     int k = atoi(argv[1]);     for(int = 0, n = strlen(message); < 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);     } } 

there no issue fgets() here. real issue here did not define array caeser properly.

you need provide size explicitly if don't initialize enough initializers @ definition time. compile-time array won't magically expand based on usage. have pre-decide size, either mentioning explicitly or initializing enough number of initalizer elements.

here, like

 unsigned char caeser[100] = {0}; 

would job you.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -