By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Urgent HELP! required for Caesar Cipher PLEASE

P: n/a
I am trying to write some code to:
1.Prompt a user for filenames
2.Open the files
3.Convert my plain text into a cipher text array/string

bear in mind I am a novice!

I have wriiten some code already which completes takes 1 and 2 but
haven't got a clue with the conversion (task 3)

The key (infile)file would contain: WGHMSAZIQRTBVCYPDJEKXFLNUO
so therefore W=A, G=B,.......O=Z
The plaintextfile would contain the text to be converted.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()

{
FILE *Key;
char c;
char infile[100];

printf("\n Input infile name:");
gets(infile);
printf("The Key is: \n");
if ((Key=fopen(infile,"r"))== NULL)
{
printf("can't open infile");
exit(0);
}

else {
do {
c = getc(Key); /* get one character from the file */
putchar(c); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF (end of file) */
printf("\n");
}
fclose(Key);
FILE *text;
char d;
char plaintextfile[100];
printf("\n Input plain text file name:");
gets(plaintextfile);

if ((text=fopen(plaintextfile,"r"))== NULL)
{
printf("can't open plain text file");
exit(0);
printf("The plain text is: \n");
}
else {
do {
d= getc(text); /* get one character from the file */
putchar(d); /* display it on the monitor */
} while (d != EOF); /* repeat until EOF (end of file) */
printf("\n");
}
fclose(text);
*/HERE IS WHERE I AM GOING WRONG*///////////////*
char a[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int ciphertext;
putchar(ciphertext);
printf("The ciphertext is: \n");

}
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Carl Harris writes:
I am trying to write some code to:
1.Prompt a user for filenames
2.Open the files
3.Convert my plain text into a cipher text array/string

bear in mind I am a novice!

I have wriiten some code already which completes takes 1 and 2 but
haven't got a clue with the conversion (task 3)

The key (infile)file would contain: WGHMSAZIQRTBVCYPDJEKXFLNUO
so therefore W=A, G=B,.......O=Z
The plaintextfile would contain the text to be converted.

<snip>

Perhaps something along these lines. Small steps for clarity. I didn't try
it.

/*convert one uppercase clear text char to upper case enciphered text
char encipher(char ch)
{
static char key[] = "WGHMSAZIQRTBVCYPDJEKXFLNUO";
/* static is for speed*/
int ix;
ix = ch - 'A'; /* make letters zero based to match key
which is already zero based */
return key[ix];
}

You can probably intuit the decipher function.

Nov 14 '05 #2

P: n/a
"osmium" <r1********@comcast.net> writes:
/*convert one uppercase clear text char to upper case enciphered text
char encipher(char ch)
{
static char key[] = "WGHMSAZIQRTBVCYPDJEKXFLNUO";
/* static is for speed*/
int ix;
ix = ch - 'A'; /* make letters zero based to match key
which is already zero based */
return key[ix];
}


That is not portable; characters are not guaranteed to have consecutive
encodings in the execution character set.

Here is my attempt. It encodes uppercase characters, and returns any
other character unencoded.
char encipher (const char c)
{
static const char plain [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char cipher [] = "WGHMSAZIQRTBVCYPDJEKXFLNUO";
const char *const ptr = strchr (plain, c);

if (ptr != NULL)
return cipher [ptr - plain];
else
return c;
}
Martin
Nov 14 '05 #3

P: n/a
Carl Harris wrote:

<snip>


void main()
int main(void)

main returns int. void is not and never has been an acceptable return
type for main.

{
FILE *Key;
char c;
char infile[100];

printf("\n Input infile name:");
gets(infile);
When posting code here, please use sane indenting, and don't use tabs.
They come out ugly if they work at all (Usenet protocols allow tabs to
be stripped from the beginning of a line, I believe). If you want people
to help with your code, the first step is to make it readable.

Also, never use the function gets(). Or, if you do choose to continue
using it, don't post code here that uses it. Also, please don't apply
for a job writing any kind of mission-critical software, or medical
software, or any kind of software for which security and correctness are
important. Consult the FAQ for more information.

printf("The Key is: \n");
if ((Key=fopen(infile,"r"))== NULL)
{
printf("can't open infile");
printf("can't open infile\n");

You need to terminate all text streams with a newline if you want your
program to be portable.
exit(0);
}

else {
do {
c = getc(Key); /* get one character from the file */
c is the wrong type if you want to use it this way. getc returns int,
and you need to use something at least as wide as int to store the
result if you hope to be able to distinguish EOF from a regular character.


putchar(c); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF (end of file) */
printf("\n");
}
fclose(Key);
FILE *text;
char d;
char plaintextfile[100];


You cannot mix declarations and executable code unless you are using
C99, which is very unlikely (since very few implementations of it exist).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4

P: n/a
Carl Harris wrote:
I am trying to write some code to:
1.Prompt a user for filenames
2.Open the files
3.Convert my plain text into a cipher text array/string

bear in mind I am a novice!

I have wriiten some code already which completes takes 1 and 2 but
haven't got a clue with the conversion (task 3)

The key (infile)file would contain: WGHMSAZIQRTBVCYPDJEKXFLNUO
Then it isn't a Caesar cipher. This is, in fact, a monoalphabetic
substitution cipher. (So is a Caesar cipher, but the Caesar cipher's key is
fixed at "DEFGHIJKLMNOPQRSTUVWXYZABC".)
so therefore W=A, G=B,.......O=Z
The plaintextfile would contain the text to be converted.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
int main(void)

{
FILE *Key;
char c;
char infile[100];

printf("\n Input infile name:");
If you want the output to appear before the program blocks for input, either
end your write to stdout with a newline, or fflush(stdout).
gets(infile);
Never use the gets() function. It's dangerous.
printf("The Key is: \n");
if ((Key=fopen(infile,"r"))== NULL)
{
printf("can't open infile");
exit(0);
}

else {
do {
c = getc(Key); /* get one character from the file */
getc returns int, not char.
putchar(c); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF (end of file) */
Make sure c is of type int, not char. Then use this loop:

while((c = getc(Key)) != EOF)
{
putchar(c);
}
printf("\n");
}
fclose(Key);
FILE *text;
char d;


Do you have a C99 compiler? If so, well, okay. If not, this arbitrary
placement of declarations should generate a compiler diagnostic.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.