caesar cipher help.

This is a discussion on caesar cipher help. within the C Programming forums, part of the General Programming Boards category; Don't know if this is the most efficient way to do it but here's a WORKING EXAMPLE: Code: #include <cstdio> ...

  1. #16
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Don't know if this is the most efficient way to do it but here's a WORKING EXAMPLE:
    Code:
    #include <cstdio>
    #include <cstring>
    
    void swapfront(char[]); 
    
    int main()
    {
        printf("Enter word:");
        char word[256]={0};
        scanf("%s",&word);
        swapfront(word);
        printf("%s\n",word);
        return 0;
    }
    void swapfront(char word[])
    {
         char f;
         f=word[0];
         char temp[256]={0};
         int i=1;
         for(int b=0;b<strlen(word);b++)
         {
         temp[b]=word[i];
         i++;
         }
         temp[strlen(word)-1]=f;
         strcpy(word,temp);
    }
    Once word is passed to the function swapfront, word now holds the new arrangement of characters.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jmd15
    Don't know if this is the most efficient way to do it but here's a WORKING EXAMPLE:
    How about one that compiles as C89?
    Code:
    scanf("%s",&word);
    Bad gets imitation. And the & shouldn't be there.
    Code:
    for(int b=0;b<strlen(word);b++)
    Don't use strlen(s) as a loop condition.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Don't use strlen(s) as a loop condition.
    It's very inefficient, looping through the whole string to find the NULL terminator for every loop of your loop.

    Code:
    printf("%s\n",word);
    And that's like puts().

    And the header files are C++ ones. And variables are declared halfway through code blocks.
    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.

  4. #19
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote Originally Posted by jmd15
    Don't know if this is the most efficient way to do it but here's a WORKING EXAMPLE:
    Code:
    #include <cstdio>
    #include <cstring>
    
    void swapfront(char[]); 
    
    int main()
    {
        printf("Enter word:");
        char word[256]={0};
        scanf("%s",&word);
        swapfront(word);
        printf("%s\n",word);
        return 0;
    }
    void swapfront(char word[])
    {
         char f;
         f=word[0];
         char temp[256]={0};
         int i=1;
         for(int b=0;b<strlen(word);b++)
         {
         temp[b]=word[i];
         i++;
         }
         temp[strlen(word)-1]=f;
         strcpy(word,temp);
    }
    Once word is passed to the function swapfront, word now holds the new arrangement of characters.
    Well heres what i have been having trouble with.
    After i get input from the user in the encode function i have. I want it to switch the first letter to the last posistion as your code above does. Then complete the decode function.
    Code:
    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);
    
    ***switch first letter to last here***
    	
    
    	{	
    		while ( buff[i] != '\0' )
    			{
    				buff[i] = buff[i] + 3;
    				i++; 
    				
    			}
    	}
    		printf("\n Your encrypted text is: %s \n", buff);
    }
    Ive just been placing the function that does the switch below the decode function. But when i try to put the code that accepts input and calls the switch function i get errors. I have tried rearranging parts of it to fit with what i want to no avail. I hope this mad sense

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    ***switch first letter to last here***
    You can do that like this:
    Code:
    size_t len = strlen(buff);
    temp = buff[0];
    buff[0] = buff[len];
    buff[len] = temp;
    [edit]
    Never mind. That doesn't do what you want.

    So you want to insert a call to swap_front() or what ever it is right there? Or what?

    No, it doesn't "mad sense".
    [/edit]
    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.

  6. #21
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    "mad sense" lol, i think im going mad trying to learn this stuff!

    basically right after the word to be encoded is accepted in the encode function from the user i want to swap the first letter to the end. example: hello-elloh

    Then complete the encode function.

    also im getting these errors

    error C2065: 'temp' : undeclared identifier

    error C3861: 'temp': identifier not found, even with argument-dependent lookup
    Last edited by stormfront; 11-19-2005 at 05:27 PM.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by stormfront
    1. Before encryption move the first letter of the message to the last posistion.( "frog" would be "rogf")
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *text)
    {
       size_t len = strlen(text);
       char first = *text;
       memmove(text, text + 1, len - 1);
       text[len - 1] = first;
       return text;
    }
    
    int main(void)
    {
       char mytext[] = "frog";
       puts(mytext);
       puts(foo(mytext));
       return 0;
    }
    
    /*
    frog
    rogf
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Man, Sorry! All the examples I have seen of scanf use it as I did. I'm not a C programmer but do know a bit of C, I use C++ so sorry about my technical errors. Sometimes I forget which board I'm on, you're lucky I didn't post C++
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #24
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    oooh, haha well im still workin on it, i need to get it done before tuesday cause thanksgiving is coming up and i wont have time to work on it. But, i still have a few days i guess.

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by stormfront
    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.
    Such that this might be an input/output pair?
    Code:
    hello world
    kiqrv ExBwp
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote Originally Posted by Dave_Sinkula
    Such that this might be an input/output pair?
    Code:
    hello world
    kiqrv ExBwp
    yes, it would output what you have above. I know i need some kind of loop, but i am not sure how to do it because we havent learned very advanced looping in my class yet. ( i know it isnt a very advanced loop lol, but it is to me!)

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So in other words:
    Code:
    letter = letter + k + loop_counter
    Right? Now you should be able to figure out the implementation of this.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote Originally Posted by quzah
    So in other words:
    Code:
    letter = letter + k + loop_counter
    Right? Now you should be able to figure out the implementation of this.


    Quzah.
    Almost, i just dont know how to check each individual letter for the loop, we have done loops like this: for(something = 0, something < \0, something ++);

    How would i check each individual letter of the text to be encoded to increase each letter by one?

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to go through each letter one at a time and print it? If so, you do the same thing, only you change it, or check it, or what not.
    Code:
    char buf[] = "Hello world!";
    int x;
    for( x = 0; buf[ x ] != '\0'; x++ )
    {
        printf( "buf[ %d ] is %c\n", x, buf[ x ] );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    got it! i'll get to work on it after class.

Page 2 of 3 FirstFirst 123 LastLast
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, 12:05 AM

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