Urgent HELP! required for Caesar Cipher PLEASE

C

Carl Harris

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");



}
 
M

Martin Dickopp

osmium said:
/*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
 
K

Kevin Goodsell

Carl Harris wrote:

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
 
O

osmium

Carl said:
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.
 
R

Richard Heathfield

Carl said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top