Thread: Caesar Cipher

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Caesar Cipher

    I need to make an encrypt/decrypt Caesar algorithm that asks for the user to put in a key. The following is what I have so far, I started off doing a rot-13 and tried to progress but as you can see, ran into a couple of problems. It is not complete yet because I have to ask for character input, but I'm having trouble seeing where I need to add a string array. Can anyone point me in the right direction and maybe give me a hint on what to do? Thanks.

    Code:
    #include<stdio.h>
    
    
    int main()
    int crypt;
    int x,y;
    int key;
    char input;
    {
    	printf("Do you want your characters encrypted [1] or decrypted [2]? \n");
    	scanf("%d", crypt);
    	printf("Enter a key");
    	scanf("%d", key);
    
        while((x=getchar())!=EOF && crypt=1)
    	{
    		printf("Enter data to encrypt.\n");
    		fflush(stdin);
        	gets(input);
    		if(x >='A' && x <='Z')
    		{
    			if((y = x + key) <= 'Z')
    				putchar(y);
    			else
    			{
    				   y = x - key;
    				putchar(y);
    			}
    		}
    		else if(x >='a' && x <='z')
    		{
    			if((y= x + key) <= 'z')
    				putchar(y);
    			else
    			{
    				y = x - key;
    				putchar(y);
    			}
    		}
    		else
    			putchar(x);
    	}
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since there is no such thing as a string array, adding one will be very difficult.

    So in your input thing, you can read a character at a time if you want, there's nothing stopping you. If you want to read in a line at a time, then make input an array of char instead of just a single char.

    And there is no correct way to use gets.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to learn about scanf.

    Code:
      scanf("&#37;d", &digit);
    
      scanf("%c", &char);
    
      scanf("%s", char array);
    The ampersand char in C means "the address of". Since array names ARE their address, they don't
    need an ampersand before them when in scanf().


    That should help.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    So, does that mean my variables are okay? I'm really confused now because I missed the class my teacher went over character arrays and the book is not helping much. I think the "gets" thing was a typo my teacher made, but he said we needed that to scan for a whole line and not just the first word.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You would then need to make x travel through each character in the input array. You would also need to encrypt using a caesar cipher correctly. (Hint: y = x - key is not using a caesar cipher correctly.)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Would it not be faster to have something like:

    Example:
    Code:
    static unsigned char ascii[256];
    
    int init(int seed)
    {
      register unsigned char *s = ascii, uc_seed = (unsigned char)seed;
      register const unsigned char *end = ascii + sizeof(ascii);
    
      for(;s < end;)
        *s++ = uc_seed++;
    }
    Then you would simply just encode by saying ascii[letter_you_wish_to_encode].

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except for "punctuation doesn't change" and "arithmetic mod 26" parts.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oh well, I guess this is what happens when you only deal with me when it is the end of the day and I am tired.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    int main()
    int crypt;
    int x,y;
    int key;
    char input;
    since when the main has such strange number of parameters?

    it should be
    Code:
    int main(void)
    {
       int crypt;
       int x,y;
       int key;
       char input;
       etc...
    fflush(stdin); - undefined - read FAQ

    crypt=1 - this is assignment you want crypt==1
    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. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 08:45 PM
  4. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 10:50 AM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM