Re: can anyone help me in correcting this code?
- From: Andrew Poelstra <apoelstra@xxxxxxxxxx>
- Date: Wed, 08 Nov 2006 14:39:57 GMT
On Tue, 2006-11-07 at 23:34 -0800, rake wrote:
hi
This program will work correctly
Hardly. This program is a nightmare in many ways.
<snip unreadable mess of letters>
#include <stdio.h>
#include <stdlib.h>
// Code for function decodeUpper(....)
Why use // comments? They restrict portability and cause syntax
errors on Usenet.
char decodeUpper(char in_c1)
{
char c1;
if (in_c1 + 19 > 'Z' )
{
c1 = in_c1 - 'Z' + 'A' - 1 ;}
else
{
c1 = in_c1 + 19;
}
return(c1);
}
Um... what? I have no idea what practical use that function could
have.
// Code for function decodeLower(....)
char decodeLower(unsigned char in_c2)
{
char c2;
if (in_c2 + 9 > 'z')
c2 = in_c2 - 'z' + 'a' - 1 ;
else
{
c2 = in_c2 + 9;
}
return c2;
}
Ditto. What's with all the magic numbers? And why subtract 'z',
add 'a', and other nonsense?
// Code for function percent(....)
float percent(int m, int n)
{
float i;
i = (float)m/n;
return( 100*i);
}
Why not use a macro? Nothing is evaluated more than once.
// Function main begins here.
main ()
int main(void)
{
// In C, string is just an array of characters terminated by '\0'.
char inString[100];
char outString[100];
int i, j;
char c;
float percentage;
int up_count = 0;
int lo_count = 0;
int num_count = 0;
int length;
char p;
Why so many variables in main? Sounds like a poorly designed
program to me.
// Read input.
printf("Enter a message string with no blanks.\n",inString);
Why did you pass that useless argument to printf()
scanf("%s",inString);
Whoa! What happens when you get more than 99 characters input?
// Write a loop for decoding characters of inString.
for(i=0,j=0; inString[i] != '\0'; i++)
{
c = inString[i];
if ( c >= 'A' && c <= 'Z' )
This is a nonsense test that means nothing.
{
up_count++;
outString[j] =decodeUpper(c);
j++;
}
else if ( c >= 'a' && c <= 'z' )
Ditto.
{
lo_count++;
outString[j] = decodeLower(c);
j++;
}
else if( c >= '0' && c <= '9' )
{
Not nonsense, but still not a common idiom. Why not use <ctype.h>
and use isdigit(), isalpha(), isupper(), islower(), et al.?
num_count++;
outString[j] = ' ';
j++;
}
}
printf("\nnUP_COUNT:%d",up_count);
Why print "nUP_COUNT"? Typos in production code destroy the
apparent value of your product (especially when combined with
setting the computer on fire).
length = num_count + up_count + lo_count;
outString[j]='\0';
// Print decoded string.
printf("Decoded Message is :%s\n", outString);
//Print num_count (count of spaces), length of decoded
printf("Length Of Decoded Message is %d\n",length);
printf("Count Of Blanks in Decoded Message is %d \n",num_count);
//Calculate % by calling function percent and print.
percentage = percent(num_count ,length); // [corrected statements]
printf("\n%f percent of output is blank\n",percentage);
}
I'm sorry for cross-posting this. It was originally in
comp.programming and was clearly more topical in comp.lang.c.
However, my newsreader (Evolution) appears to have no ability
to set followups. Can anyone help me?
--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.
.
- Follow-Ups:
- Re: can anyone help me in correcting this code?
- From: CBFalconer
- Re: can anyone help me in correcting this code?
- Prev by Date: Re: array name and its address. Can they be same
- Next by Date: Re: $100 Bill Club
- Previous by thread: array name and its address. Can they be same
- Next by thread: Re: can anyone help me in correcting this code?
- Index(es):
Relevant Pages
|