473,372 Members | 921 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,372 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 8016
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.