Re: Exercise 7-1
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 03 Sep 2008 00:35:23 -0700
mdh <mdeh@xxxxxxxxxxx> writes:
On Sep 2, 8:53 pm, Keith Thompson <ks...@xxxxxxx> wrote:
mdh <m...@xxxxxxxxxxx> writes:
I am not sure how relevant to C this is, but here goes.
.
Your indentation is inconsistent and doesn't correspond either to the
actual structure of your code or to the intended structure of your
code.
Sorry about the formatting.I will have to figure that out sometime.
Here's your program after it's been filtered through "indent -kr" (and
after I manually joined the split line):
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int c;
if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/upper") == 0)
while ((c = getchar()) != EOF)
putchar(toupper(c));
else
putchar(tolower(c));
return 0;
}
This should be enough to tell you what the problem is.
The solution, except for the omission in error of a second "while ((c
= getchar()) != EOF)" is from Tondo and Gimpel.
That would be "The C Answer Book". If you're going to cite books
other than K&R, please be specific; I had to look that up.
I think it is really
simple to illustrate the issue that K&R are trying to make, instead of
making it bullet proof?
while ((c = getchar()) != EOF){
putchar(toupper(c));
}
else {
while ((c = getchar()) != EOF)
putchar(tolower(c)); }
<<<<<
But, have I missed something. Is that the reason one needs a 2 step
invocation of this program?
(Does the subject "Exercise 7-1" refer to K&R? My copy isn't handy.)
I'm afraid I can't tell from your response whether you understood my
point or not. In the code you've marked with ">>>>>" and "<<<<<", you
have an "else" with no corresponding "if". If that's a fragment of
Tondo and Gimpel's solution, it's a poorly chosen fragment.
If I understand correct, the program is supposed to convert its input
to upper case if it's invoked as "upper", and to lower case otherwise.
The problem is that, due to the way you've structured the code, if
it's invoked as "upper" (or rather as "/Users/.../upper") it uses a
while loop to read each character from stdin, otherwise it reads only
a single character.
There are two obvious ways to do what you're trying to do.
In pseudo-code, with structure shown only by indentation, you can do
either:
if my name is "upper"
for each input character
print that character in upper case
else
for each input character
print that character in lower case
or:
for each input character
if my name is "upper"
print that character in upper case
else
print that character in lower case
What you've done is an incorrect mixture of the two approaches.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: Exercise 7-1
- From: Ron Ford
- Re: Exercise 7-1
- References:
- Exercise 7-1
- From: mdh
- Exercise 7-1
- Prev by Date: Re: printf
- Next by Date: Re: detecting ASCII/EBCDIC
- Previous by thread: Re: Exercise 7-1
- Next by thread: Re: Exercise 7-1
- Index(es):
Relevant Pages
|