Thread: Caesar Cipher Trouble with spaces

  1. #1
    Registered User
    Join Date
    Oct 2013

    Caesar Cipher Trouble with spaces

    I am trying to create a program that does the caesar cipher on a sentence. The program i have works on a single word but when i input a sentence into the code it doesn't complete any of the code and my guess would be because of the addition of the space. Im sorry if it doesn't make complete sense because this is my first class ever programming.

    The code has to use loops and arrays.

        int i;int key = 0;char message[80];    printf("Enter message to be encrypted: ");
        scanf("%s", message);
        printf("Enter shift amount (1-25): ");
        scanf("%d", &key);
        for(i = 0; message[i] != '\0';i++)
            if('A'<= message[i] && message[i] <= 'Z')
                {message[i] = (((message[i]-'A') + key) % ('Z' - 'A' + 1) + 'A');}
            else if('a'<= message[i] && message[i] <= 'z')
                {message[i] = (((message[i]-'a') + key) % ('z' - 'a' + 1) + 'a');}
    printf("Encrypted message: %s", message);

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Under the moon
    Look into the function fgets rather than scanf, since scanf stops reading when it reaches white space.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Caesar Cipher help!!
    By darshan10 in forum C Programming
    Replies: 6
    Last Post: 10-19-2011, 04:58 PM
  3. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  4. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 08:45 PM
  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