473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Urgent HELP! required for Caesar Cipher PLEASE

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
4 8015
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Stefan Bauer | last post by:
Hi NG, we've got a very urgent problem... :( We are importing data with the LOAD utility. The input DATE field data is in the format DDMMYYYY (for days) and MMYYYY (for months). The target...
0
by: Amratash | last post by:
Hi, I needyour help.Its urgent. My main aim is to log the site activities in a database at runtime. I'm using W3C Extended Log File format for logging information. I can use ODBC Logging directly...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
7
by: zeyais | last post by:
Here is my HTML: <style> ..leftcolumn{float:left;width:300px;border: 1px solid #ccc} ..rtcolumn{float:left;width:600px;border: 1px solid #ccc} </style> <body> <div class="leftcolumn"...
2
by: Xeijin | last post by:
URGENT I have an assignment to hand in tomorrow, I need to know how to perform numeric calculations in access, I dont know very much about databases so consider this a beginner's query! Well...
8
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
2
by: Max Power | last post by:
Hi All I am coding a small app in that swaps specific files between a client and server. All files and locations are set at both sides. I want my app to show a file list, based on the file...
13
by: Niyazi | last post by:
Hi I have a report that I have to run it monthly in my machine. My code in VB.NET and I access AS400 to get data, anaysie it and send into pre formated Excel sheet. The data consist of 9000...
0
by: IT Jobs | last post by:
Hi We are from Ventures IT Solutions, A Young Organisation based in Delhi, and is started with a vision to provide, quality services to our esteemed clients and candidates, We have some...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.