Thread: an encrypted program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    an encrypted program

    hello fellows, i got new project for my c class but i have failed to do it. i would be very appreciated if you help to do this program.her it comes:

    Sample Run 1:
    input 1: coggvogcvvgpwpfgtdtkfig
    output 1: ameetmeattenunderbridge

    Sample Run 2:
    input 2: oushpcaptfcanshohashfcghohwcb
    output 2: agetbombfromzetatmetrostation

    Sample Run 3:
    input 3: vorjhzincjonziyhzydx
    output 3: atwomenshotsendmedic

    i am sending you the original project sheet paper :
    C PROJECT

    Lieutenant, we are catching encrypted messages from enemy lines. Our mathematicians solved the
    encryption pattern. A message like:
    coggvogcvvgpwpfgtdtkfig
    ameetmeattenunderbridge
    This is a modified version of Caesar encryption. First character is used to define SHIFT: distance of
    first character to letter ‘a’. If all other characters are shifted with the same amount, message appears
    (here, shift value is -2 (from letter ‘c’ to letter ‘a’)). Shifting is also cyclic, meaning that if we shift ‘c’ for -
    (‘c’->‘b’->’a’->’z’->’y’)
    Right now, our mathematicians are decrypting messages by hand with all their efforts but we need a C
    program from YOU to rapidly decrypt all the incoming messages.
    • Input will only consist of the encrypted message, and enter (‘\n’) at the end.
    • Your output MUST only consist of decrypted message (see sample runs below) and one enter
    (‘\n’) at the end
    • See sample runs ABOVE for more details

    İ WOULD BE REALLY APPRECATED İF ANY OF YOU HELP TO DO THİS PROJECT

    THANX

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    No one here is going to do your homework/project/assignment for you!
    Show your effort.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Quote Originally Posted by Bayint Naung View Post
    No one here is going to do your homework/project/assignment for you!
    Show your effort.
    i know that sir, i just want some help from experinced c programmers.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, I'd code up a function or two to handle the Caesar shift. If you don't have a clue about that, Wikipedia has a good write up on it. (and lots of other algorithms).

    Get that working well, and then add in your functions for the reading of the input and make sure your output meets the requirements, as well.

    Right now, we can't help you much, because you haven't studied the problem, and produced questions that show what has you stumped, at the moment.

    When you have detailed questions, about specific code or logic that we can see, then we can be much more helpful.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, better hurry your effort too... It sounds like there is a war at stake.

    On a sidenote I remember that when I was in college we had to write a very similar program in MIPS assembly language which was far more challenging than doing it in C. That's just to encourage you a bit
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Quote Originally Posted by makonikor View Post
    i know that sir, i just want some help from experinced c programmers.
    Simply pasting in the assignment text is not "asking for help", it's asking for someone to do it for you.

    If you were actually interested in learning any of this, you'd start to write some code and then paste the code you're stuck on or have problems with in here, asking for specific help. THAT'S how you ask for help. What you do is ask for a service. (And, of course, if you want to pay for it, someone's guaranteed to want to help you :-)

    And use the code-tag when posting code, so that it's easier to read and more tidy allover.
    Last edited by cnewbie1; 07-12-2010 at 09:42 AM.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    I am also trying to learn C programming, but so far i have not been very successful. The main reason is that i find the assignments in many C programming books very boring, and therefore fail to solve them. The projekt posted in this thread seemed very interesting, and i could not resist to try solving it. The program is properly not very well written, but i am quite happy with it. I did not create a separate function for input as i could not figure out to return a array pointer to main. The message can maximum contain 100 characters. I had alot of fun with solving the shift cyclic.

    decrypt_message.c

    Code:
    #include <stdio.h>
    
    #define MESSAGE_MAX_SIZE 100
    
    void caesar_shift(char encrypted_message[MESSAGE_MAX_SIZE], int character_counter)
    {
    
            int result = 0;
            int result_counter = 0;
    
            int shift = encrypted_message[0] - 'a';
    
            for(result_counter = 0; result_counter < character_counter; result_counter++)
            {
                    result = encrypted_message[result_counter] - shift;
    
                    if(result < 'a')
                    {
                            result = ('z' + 1) - ('a' - result);
                    }
    
                    printf("%c", result);
            }
    
            printf("\n");
    }
    
    
    main()
    {
    
            char encrypted_message[MESSAGE_MAX_SIZE];
    
            int character = 0;
            int buffer_overflow_check = 0;
            int character_counter = 0;
    
            while(character != '\n' && buffer_overflow_check < MESSAGE_MAX_SIZE)
            {
                    character = getchar();
    
                    encrypted_message[buffer_overflow_check++] = character;
                    character_counter++;
            }
    
            caesar_shift(encrypted_message, character_counter);
    }
    The program do have a small flaw, as an ekstra character is appended to the message:

    Sample run 1:

    Code:
    coggvogcvvgpwpfgtdtkfig
    ameetmeattenunderbridge"
    Does anyone know why this is happening?

    If anyone has links to C learning projekts like the one in this thread, please post them.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good effort, but it seems that you have a loop for the message logic, and yet, in Caesar shift function, you use another loop. Shouldn't one loop be controlling the message, and the other function just do the "shifting" of ONE letter?

    That's my mantra these days - always keep your logic as simple as you can. If you don't need two loops (and you don't), then don't have two loops.

    Also, just being picky, the assignment wants the newline, appended onto the end of the char array, not just printed out.

    I like the way you found the relationship between the letters in the shifting, though.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Quote Originally Posted by Adak View Post
    Good effort, but it seems that you have a loop for the message logic, and yet, in Caesar shift function, you use another loop. Shouldn't one loop be controlling the message, and the other function just do the "shifting" of ONE letter?

    That's my mantra these days - always keep your logic as simple as you can. If you don't need two loops (and you don't), then don't have two loops.

    Also, just being picky, the assignment wants the newline, appended onto the end of the char array, not just printed out.

    I like the way you found the relationship between the letters in the shifting, though.
    Thanks for the critique, this is always a great way to learn something new.

    I do have one while loop in main that fill the encrypted_message array with characters from an input source, and checking for newline and overflow of the buffer. My first thought was to create a function called get_message() that would handle the input, but as arrays was involved i could not get this to work, so i handle this in main instead. The for loop in the function caesar_shift() do the shifting and output of the result. With my current knowledge in C, to reduce the two loops to one i could put the whole thing in main, but when using functions i would not know how to use only one loop?

    Regarding the newline, i do not read it as they want it appended to the array:

    Code:
    Your output MUST only consist of decrypted message (see sample runs below) and one enter (‘\n’) at the end
    If they really wanted the newline appended to the array, i guess i could create an if statement that checked for the last character in the array, and the inserted a newline after that, but this would be more complicated that just printing it at the end.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's great that you solved the problem but you shouldn't have posted your entire code here for the OP to prey on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Quote Originally Posted by claudiu View Post
    It's great that you solved the problem but you shouldn't have posted your entire code here for the OP to prey on.
    Maybe not the best idea to post it in full, but for others to come with suggestions why the ekstra character is appended to the result, they should see the code.

  12. #12
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17
    Good work and great attitude, kasj!

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    kasj: Here's a very good article explaining how to return arrays from functions. Or rather, how to work around it, since just returning an array from a function (which is easy in f.ex. Java, PHP and "newer" high-level languages) isn't possible in C. http://www.eskimo.com/~scs/cclass/int/sx5.html

    By the way: I wholehartedly agree with your comment about the typical training assignments in most programming books. Problem is, when you write code for a living, most of the problems aren't too interesting in themselves then either :-)
    Last edited by cnewbie1; 07-13-2010 at 11:38 AM.

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Great article. I tried the examples and i must admit that it is very simple to "return" arrays, simply making the array static will be enough in some cases. I have tried to change my program with the new knowledge, i have used method two in the article by passing an array as an argument to my new function.

    I have also solved the problem with the ekstra character that was appended to the output, at the solution was very simple. When entering a message and pressing enter, the variable character_counter vil of course count the newline, and then when function caesar_shift() prints out the array, an ekstra character will be displayed. So now i just substract 1 from character_counter before returning. The main() function is really clean now

    decrypt_message.c

    Code:
    #include <stdio.h>
    
    #define MESSAGE_MAX_SIZE 100
    
    int get_message(int encrypted_message[MESSAGE_MAX_SIZE])
    {
    
            int character = 0;
            int buffer_overflow_check = 0;
            int character_counter = 0;
    
            while(character != '\n' && buffer_overflow_check < MESSAGE_MAX_SIZE)
            {
                    character = getchar();
                    encrypted_message[buffer_overflow_check++] = character;
                    character_counter++;
            }
    
            return character_counter - 1;
    }
    
    void caesar_shift(int character_counter, int encrypted_message[MESSAGE_MAX_SIZE])
    {
    
            int result = 0;
            int result_counter = 0;
    
            int shift = encrypted_message[0] - 'a';
    
            for(result_counter = 0; result_counter < character_counter; result_counter++)
            {
                    result = encrypted_message[result_counter] - shift;
    
                    if(result < 'a')
                    {
                            result = ('z' + 1) - ('a' - result);
                    }
    
                    printf("%c", result);
            }
    
            printf("\n");
    }
    
    
    main()
    {
            int encrypted_message[MESSAGE_MAX_SIZE];
    
            caesar_shift(get_message(encrypted_message), encrypted_message);
    }
    Output:
    Code:
    coggvogcvvgpwpfgtdtkfig
    ameetmeattenunderbridge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM