Thread: How to use fgets() properly? (Caesar Cipher - Encryption program)

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    6

    How to use fgets() properly? (Caesar Cipher - Encryption program)

    The feedback to my last code example was great, that's why I want to ask a few things again.
    I wrote a program that applies the so-called Caesar shift(+13) to a input text.

    But I wonder how I should use fgets() better. First I've used gets(), but this method gives you uncontrolled array overflows. So I switched to fgets() and figured that when the array size is smaller then the amount I put in, fgets() always write so much characters in the array that is possible and the rest stays in the input buffer.

    I used the 2nd answer for my problem - fseek: How to clear input buffer in C? - Stack Overflow

    Of course I could just increase the buffer to a huge amount, but what do you say? What is the best solution for this?

    How I understand fseek, I put the FILE* Pointer to SEEK_END with zero offset, so the data is still in the buffer. I guess with a new fgets-call it will be get deleted?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_LENGTH 3
    #define SHIFT 13
    
    void Caesar(char s[]);
    
    int main(void)
    {
      char s[MAX_LENGTH];
      int c;
    
      puts("\nType in the text(max. 100 characters) you want to encrypt: ");
    
      while(fgets(s, MAX_LENGTH, stdin) != NULL) // reads string with certain length "MAX_LENGHT" from stdin and writes it in character array s
      {
        if (s[strlen(s) - 1] == '\n') // delete the added newline character '\n' and replace it with terminating zero '\0'
          s[strlen(s) - 1] = '\0';
    
        puts("\nEncrypted: ");
        Caesar(s);
        puts(s);
    
        puts("\nDecrypted: ");
        Caesar(s);
        puts(s);
    
        puts("\nType in the text(max. 100 characters) you want to encrypt: ");
    
        fseek(stdin, 0, SEEK_END);
      }
    
    
      return 0;
    }
    
    /*
    * Encrypts text by replacing each character with its 13th right neighbour.
    * By applying Caesar() two times on a text gives you the original string.
    */
    void Caesar(char s[])
    {
      int buffer=0;
      int i=0;
    
      while (s[i] != '\0')
      {
        if (s[i] >= 'a' && s[i] <= 'z') //only encrypt/decrypt for lower-case letters
        {
          s[i] = (s[i] - 'a' + SHIFT) % 26 + 'a'; //encrypt (or decrypt) the letter s[i]
        }
        i++;
      } 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > puts("\nType in the text(max. 100 characters) you want to encrypt: ");
    Perhaps making MAX_LENGTH 100 instead of 3 would help.

    The construct
    while ((c = getchar()) != '\n' && c != EOF);

    is the only sure-fire way of disposing of the remains of the current line.

    Keep reading SO, everything else is non-portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    6
    I see, thanks.

    I.e. this construct you've posted reads each single character from input-stream till a '\n' AND a EOF is reached? But how can c be a '\n' and EOF at the same time?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Revise your boolean truth tables.
    It loops until either ch is \n or ch is EOF
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program to caesar cipher file to output, help needed
    By SaraTerry in forum C Programming
    Replies: 8
    Last Post: 11-29-2012, 03:45 PM
  2. I need help on what I think is a Caesar cipher......
    By CaesarCipher in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2011, 04:13 PM
  3. Caesar Cipher help!!
    By darshan10 in forum C Programming
    Replies: 6
    Last Post: 10-19-2011, 04:58 PM
  4. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  5. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 10:50 AM

Tags for this Thread