Caesar Cipher Trouble with spaces

This is a discussion on Caesar Cipher Trouble with spaces within the C Programming forums, part of the General Programming Boards category; I am trying to create a program that does the caesar cipher on a sentence. The program i have works ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    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.

    Code:
        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);
    
    return0;

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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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, 07: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21