473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help...........

I have a small C program for a college course. It is meant to encrypt
and decrypt lower case letters and leave spaces as spaces. I can't
get it to run properly as I think I have a problem in the While
construct area. It is driving me mad trying to sort it out - can
someone please help.

The program is:

/*
* Program to encrypt a message using the Turbo C random
* number generator. Only lower case letters and the space
* character can be used in the message.
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>

#define SEED 1

unsigned char encrypt(unsigne d char);
unsigned char decrypt(unsigne d char);
void wait(void);
main()

{
unsigned char c;
char type;

/*
* initialise the random number generator
*/
srand(SEED);

/*
* display user headings and prompts
*/
clrscr();
cprintf("T223 encryption program\r\n\r\n ");
cprintf("Type d to decode a message or any other character to encode
");
type = getch(); /* input the user option */
cprintf("\r\nNo w type in the message, use only lower case letters "
"or space.\r\nEnd with any other character.\r\n\ r\n");
c = (unsigned char)getchar(); /* read in first character */

/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while ((c>='a') && (c<='z') && (c=' '));
{
if (c==' ')
{
switch (type)
{
case '64':
c = decrypt (c) ;
break;

default:
c = encrypt (c) ;
break;
}
}
putchar('c'); /* display encrypted/decrypted character */
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
wait();
}

/*
* end of main() function
*/
/*
* function encrypt()
* =============== ===
* encrypts a single character
*/
unsigned char encrypt(unsigne d char letter)
{
unsigned char r;
r = (unsigned char)random(26) ;
letter = letter + r;
if (letter>'z')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/
/*
* function decrypt()
* =============== ===
* decrypts a single character
*/
unsigned char decrypt(unsigne d char letter)
{
unsigned char r;
r = (unsigned char)random(26) ;
letter = letter - r;
if (letter<'a')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
/*
* function wait()
* ===============
* waits for a keypress
*/

void wait(void)
{
fflush(stdin);
cprintf("\r\nPr ess return to terminate");
getchar();
}
/*
* end of wait()
*/
/*
* end of program
*/
Nov 14 '05
11 1529
On Wed, 25 Feb 2004 22:01:42 UTC, pete <pf*****@mindsp ring.com> wrote:
The Real OS/2 Guy wrote:

On Wed, 25 Feb 2004 14:01:38 UTC, pete <pf*****@mindsp ring.com> wrote:
I've got this:

#include <limits.h>
#include <string.h>

#define LOWER "abcdefghijklmn opqrstuvwxyz"

int islower(int c)
{
return CHAR_MAX >= c && c > '\0' && strchr(LOWER, c) != NULL;
}


try to test the strings

"MÄHMASCHIN E"
"mähen"
"büßen"
......

fails miserably on characters used by french, italian, german,
norways, .........
The world is not US only.

islower() works correctly on them when LC_LOCALE is set correctly.


I don't have LC_LOCALE in my version of N869.

Is it possible for a conforming implementation to only support
the "C" locale ?

Yes - but that limits you to be unable to NOT use national language
support and makes your program userunfriendly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #11
On Wed, 25 Feb 2004 11:49:55 +0000, Richard Heathfield
<in*****@addres s.co.uk.invalid > wrote:
Michael wrote:
Hi Steve,
Peter already pointed out some problems
in your program (more I ever found). I
just want to add s.th. according your
while condition:
while ((c>='a') && (c<='z') && (c=' '));
Isn't it
> while ((c>='a') && (c<='z') || (c==' '))

a character 'or' space?


Well, that's certainly an improvement, since no character can be a letter
/and/ a space. But this:

Plus it (quietly) fixes the spurious null loop body.
while(islower(( unsigned char)c) || (c == ' '))
Although in general the cast is advisable, in the OP's code c is
(already) uchar, plus set from getchar which except for EOF (not
checked for) produces only uchar *values* though not uchar type.
is better still, since it doesn't rely on lower case letters being
contiguous and in alphabetical order.


Although the encrypt and decrypt functions, which you snipped, do.
Plus they rely on the codes for lowercase letters not being within 26
of either end of the range of unsigned char. Which does happen to be
the case for ASCII. And even for EBCDIC, which already failed the
contiguous criterion anyway, and doesn't satisfy that for *upper*case.

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1703
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web browser that Hotmail does not support. If you continue to use your current browser software we cannot guarantee that Hotmail will work correctly for you". Please help, this is very annoying. I have been searching for help on
7
3616
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title> </head> <style type="text/css">
7
3300
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that I believe if it's solved, the remaining stuff is easy... my full program until now is here: http://www.geocities.com/tom4_h4wk/progmail.zip the problem is the segmentation fault when main trys to run leficheiro.c.... the *.c2 files are the...
23
3290
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
13
4337
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error "operation is not allowed when object is open" so I take out the line of code: BookDetails.Connection1.Open and it comes up with the error "operation is not allowed when object
1
9657
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
54542
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and we instruct our experts to ignore any such PMs completely Be sure to give the version of Access that you are working with and the Platform and OS if applicable.
0
3057
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
3328
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID, EmpName,DeptID,DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName, NoOfEmployees)...
5
2322
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks for user name, check in the system but does not return the correct answer please help me with it. or if you have better way of doing it would you please mind to tell me.. thanks.. #!/usr/bin/perl -w #use Getopt::Std;
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.