help with my ceaser cipher program

This is a discussion on help with my ceaser cipher program within the C Programming forums, part of the General Programming Boards category; hi. I am trying to write a program that changes each character in a text file by moving each character ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    help with my ceaser cipher program

    hi. I am trying to write a program that changes each character in a text file by moving each character along a by a set number in the alphabet but am completely stuck. I have done the program that reads in a text file and writes one out but I dont now how to do the bit in the middle where each character gets shifted along between 1 and 26 spaces? how can I convert each letter in the alphabet to a number? what variables do I need? any help greatly appreciated!
    here is my code so far:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include < string.h >
    //a program that reads in a text file chosen by the user character by character and writes it to a new file (I got stuck on the offset key bit) 16.12.10
    void main ()//defines a program called main
    {
    FILE *file_in;//defines the stream as file in
    FILE *file_out;//defines a stream as file out
    char letter, destination[30]; //defines a string for each character and and array for the name of the text file
    int offset = 0;
    printf("please enter the off set key amount\n");
    
    printf("please enter the file destination you want to read in\n");//gets the user to enter a file
    scanf("%s%d" , destination , offset);//reads in the destination and stores it in destination variable
    
    file_in=fopen(destination, "r");// opens and reads the file
    file_out=fopen("newtest.txt", "w");
    do//reads in the file and prints the file untill the end of file condition is met.
    {
    
    letter=getc(file_in);//reads each letter in one at a time to the letter variable
    fprintf(file_out, "%c" , letter);//prints each letter out on the screen
    }
    while
    
    (letter != EOF);//checks if has reached the end of the text file
    fclose(file_in);
    fclose(file_out);//closes the stream
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would ignore reading and writing from a file right now, and just do it in memory. Example:
    Code:
    Enter a sentence to encode:
    <you enter something>
    Your sentence is now:
    <output of encoded version>
    Once you have that, then start worrying about reading and writing the file.

    Work through the above program in words, what it is you think you need to do:
    Code:
    read a sentence into something that will store a sentence
    pass that to a function to encode it
    output the encoded sentence
    Now break each of those into smaller steps, in words, until you have an idea of what you need to do to turn it into code.


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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    sorry mate i need more help. been stuck for hours, i know i got to write a function that will encode an array but i dont know how to do it. can you start me off please?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void encodearray( char array[], size_t len )
    {
        ...your code here...
    }
    Called like so:
    Code:
    char myarray[ BUFSIZ ];
    ...
    encodearray( myarray, BUFSIZ );
    There, you're started.

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

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quzah's approach would be better if you didn't have any code written yet, but since you've already written the I/O portion, I would keep it (with some modifications).

    Some things you should fix first:
    1. Fix your formatting/indenting next time you post code so we can read it.
    2. It's int main(void), and you need a return 0; at the end of main once you change that.
    3. Your scanf has several problems. The order doesn't match the prompts, it's going to expect all the data on one line and your offset parameter needs an ampersand (read the documentation!)
    4. Check file_in and file_out for NULL in case fopen fails.
    5. Your do-while loop will write the EOF character to the output file. Make it a while ((letter = getc()) != EOF) loop.

    As for your Caesar cipher, you want to normalize each character, so a is 0, b is 1, etc. Then you're going to add offset to it and do modulo 26 to make it wrap around. Then un-normalize it so that 'A', 'B', 'C', etc have their proper ASCII values. Then, write that character to the output file.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well it doesn't really matter if he does anything with the file or not. He actually doesn't even need to use an array, since all he really needs is:
    Code:
    unenc = fgetc( input );
    enc = encrypt( unenc );
    fputc( enc, output );
    edit, in reply to the above post: No, I want you to actually think about the problem, not just have me think about it for you. What do YOU think you need to do?

    Quzah.
    Last edited by quzah; 12-16-2010 at 04:26 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, understand that every letter (and every number), and every char you have for your computer, IS A NUMBER.

    Your computer runs on numbers, not letters, not digits (what you call numbers), that you see on the screen -- NUMBERS. Those numbers are part of the "character set" for your system, and it's usually similar to ascii values.

    You should google (ascii table), and get an ascii table, for your reference. As a programmer, you'll need it.

    So you have a string of char's, and say for simplicity, you want to shift them up one number:
    Code:
    s[]= {"May you bloom and grow"}; //from "Edelweiss"
    c[]= {"Nbz zpv cmppn boe hspx"}; //every letter shifted up one number
    /* spaces were left alone */
    So 99% of the work is just to scan through each letter of the string, and increase it by the amount of the shift. 1% of the work will be "wrapping it around" when the shift causes the value to be greater than a 'z'. In those cases you assign the value immediately to 'a' (or 'A' if it's an uppercase letter), and then continue adding the remaining values of the shift.

    In a for loop is common, but a while loop will do it nicely
    Code:
    len = strlen(s);
    start with the first letter: s[i]
    for each letter in s[] to len;
      if s[i] is a ' ' 
         continue; //do nothing with spaces
      increment s[i] by by the amount of the shift
      if the new value is greater than the highest letter in that case (z or Z), 
      then set it's value to a or A, and continue incrementing, just as before
    
    end of for loop
    This is a very simple process -- don't over complicate it. It's straight forward!
    For strlen to work, you should include <string.h> header file.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    i need a function that:
    moves the cursor along one character at a time
    converts that character into a number between 1 and 26
    adds number X to that number
    converts that number back into a character
    stores all the new charters.

    i just dont have a clue how to do it? out teacher told us not to use ascai values?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by tombocollbo View Post
    i just dont have a clue how to do it?
    Give it a shot. You already have file reading/writing loop serves the purpose of "moving along one character at a time" and "stores all the new characters". I'm sure you know or can figure out how to add and subtract two things.

    out teacher told us not to use ascai values?
    What the teacher means is that, if you look at an ASCII chart, you will see that 'A' has a numeric value of 65. The teacher is saying don't use the number 65, use the character literal 'A', which in ASCII is 65, but in other encoding schemes (UTF, EBCDIC, etc) may have a different numeric value. Your program will map 'A' to the right numeric value for your given encoding scheme.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    thanks for the help. i think im on the right track now. i got to sleep now. my brain feels like its been microwaved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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