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 ...

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
}

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.

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:

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.

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?

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.