Thread: caesar cipher help.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    caesar cipher help.

    I want to add a few things to this caesar cipher program but am not exactly sure how. They are:

    1. Before encryption move the first letter of the message to the last posistion.( "frog" would be "rogf")

    2.replace each letter by a constantly increasing value of k. So,
    if k is 3, replace the first letter of the message by the letter that comes 3 positions after it. Next,
    replace the second letter of the message by the letter that comes 4 positions after it. Next, replace
    the third letter of the message by the letter that comes 5 positions after it, and so on.

    3.At the beginning ask the user the value they want to use to encrypt the code. (It is currently set up at 3) But, i want it to be an option for the user.

    4. Let the code "wrap around" when i type in zebra instead of returning cheud, it gives a funky character for z.

    Not sure how to do these, they seem simple i just cant come up with how to do them.


    Code:
    #include <stdio.h>
    
    void menu ( void ) {
        printf("Please enter a number: \n"
               "1-Encrypt\n"
               "2-Decrypt\n"
               "3-Exit\n"
               "prompt > ");
        fflush( stdout );
    }
    
    int getchoice ( void ) {
        char buff[BUFSIZ];
        int  choice = 0;
        do {
            menu();
            if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
                /* success reading a line, does it make sense? */
                if ( sscanf( buff, "%d", &choice ) != 1 ) {
                    printf( "Enter a number\n" );
                }
            } else {
                /* user EOF, just exit now */
                choice = 3;
            }
        } while ( choice < 1 || choice > 3 );
        return choice;
    }
    
    void encode ( void ) 
    {
    	char buff[BUFSIZ];
    	int i = 0;
                    printf( "Doing encrypt\n" );
    	printf("\nPlease enter the text you wish to encrypt: ");
    	
    	fgets(buff, sizeof(buff), stdin);
    	{	
    		while ( buff[i] != '\0' )
    			{
    				buff[i] = buff[i] + 3;
    				i++; 
    				
    			}
    	}
    		printf("\n Your encrypted text is: %s \n", buff);
    }
    void decode ( void ) 
    {
    	char buff[BUFSIZ];
    	int i = 0;
                    printf( "Doing decrypt\n" );
    	printf("\nPlease enter the text you wish to decrypt: ");
    	
    	
    	fgets(buff, sizeof(buff), stdin);
    	{
    		while ( buff[i] != '\0' ) 
    			{
    				buff[i] = buff[i] - 3;
    				i++;
    			}
    	}
    		printf("\nYour decrypted text is: %s \n", buff);
    }
    
    int main ( ) {
        int choice;
        while ( (choice=getchoice()) != 3 ) {
            if ( choice == 1 ) {
                encode();
            } else
            if ( choice == 2 ) {
                decode();
            }
        }
        return 0;
    }
    Last edited by stormfront; 11-18-2005 at 06:00 PM.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    In the include file algorithm, there are many useful functions that could help you. One of these being swap(). Used as such:
    Code:
    ....
        char msg[25]={0};
        strcpy(msg,"Hello");
        swap(msg[0],msg[(strlen(msg)-1)]);
    Now instead of msg holding Hello it will hold oellH. I would also check out some of the other algorithms available.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote Originally Posted by jmd15
    In the include file algorithm, there are many useful functions that could help you. One of these being swap(). Used as such:
    Code:
    ....
        char msg[25]={0};
        strcpy(msg,"Hello");
        swap(msg[0],msg[(strlen(msg)-1)]);
    Now instead of msg holding Hello it will hold oellH. I would also check out some of the other algorithms available.

    thx, but i just want it to swap the first letter to the end.
    example: "hello" would be "elloh"
    Is there an easy way to implement this ?

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Oh nevermind, I thought you wanted to swap the two.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Just use a loop and walk through your array replacing each current element with what is in the current element + 1. Of course you will want to save the original value of element 0 in a temp variable, and then when you are done 'shifting' all of your characters, you will want to place the character in temp in the last position in your array.

    Give it a try, and post your code if you can't get it.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Is this what you wanted?

    Code:
    void swapit(char *pInput)
    {
        char b, e;
        b = pInput[0];
        e = pInput[strlen(pInput)-1];
        pInput[0] = e;
        pInput[strlen(pInput)-1] = b;
    }

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    that appears to be what i need but when i try to put it in my code and call it, it doesnt do anything....maybe im doing wrong. Where does it go?
    Last edited by stormfront; 11-18-2005 at 06:57 PM.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Example...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void swapit(char *pInput)
    {
        char b, e;
        b = pInput[0];
        e = pInput[strlen(pInput)-1];
        pInput[0] = e;
        pInput[strlen(pInput)-1] = b;
    }
    int main(void)
    {
        char szSwap[] = {"Hello"};
        printf("%s\n", szSwap);
        swapit(szSwap);
        printf("%s\n", szSwap);
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    hmm, no im still getting oellH using the swap function.

    "Just use a loop and walk through your array replacing each current element with what is in the current element + 1. Of course you will want to save the original value of element 0 in a temp variable, and then when you are done 'shifting' all of your characters, you will want to place the character in temp in the last position in your array."


    this seems like my best bet
    Last edited by stormfront; 11-18-2005 at 07:13 PM.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Possibly this???
    Code:
    #include <stdio.h>
    #include <string.h>
    char * swapit(char *pInput)
    {
       static char szTemp[128];
        char b;
        b = pInput[0];
        memset(szTemp, 0, sizeof szTemp);
        strcpy(szTemp,pInput+1);
        szTemp[strlen(szTemp)] = b;
        return  (szTemp);
    }
    int main(void)
    {
        char szSwap[] = {"Hello"};
        printf("%s\n", szSwap);
        printf("%s\n",swapit(szSwap));
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Thats it, but how do i get it to accept What the user inputs instead of hello?

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Read the FAQ.

    ~/

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    im working on it...slowly
    Last edited by stormfront; 11-18-2005 at 08:02 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char szSwap[] = {"Hello"};
    You don't need the curly braces.

    Code:
    static char szTemp[128];
        char b;
        b = pInput[0];
        memset(szTemp, 0, sizeof szTemp);
    You can just use ={0}.

    Did you get it to work, stormfront?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    No, still cant get it to work in the code. : (

    i know i must be trying to put it in the wrong place

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. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 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