Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!uunet!charyb!will From: will@kfw.COM (Will Crowder) Newsgroups: comp.lang.c Subject: Re: Is there a good example of how toupper() works? Message-ID: <1990Oct17.165509.10914@kfw.COM> Date: 17 Oct 90 16:55:09 GMT References: <2466@ux.acs.umn.edu> <15857@csli.Stanford.EDU> <1990Oct16.221035.10764@nntp-server.caltech.edu> Reply-To: will@kfw.com (Will Crowder) Organization: KFW Corporation, Newbury Park, CA Lines: 54 In article <1990Oct16.221035.10764@nntp-server.caltech.edu> bruce@seismo.gps.caltech.edu (Bruce Worden) writes: >I sent the original poster this code with an explanation, which people may >comment on as they see fit: > >#include >main() { > char *duh = "Hello"; > int i, limit = strlen(duh); > printf("%s\n", duh); > for(i=0; i if (islower(duh[i])) duh[i] = toupper(duh[i]); > } > printf("%s\n", duh); >} > >P.S. Why did I rewrite the `while' loop above as a `for' loop? I have >found `for' loops to be very efficient (if that is a consideration) and, >as I have said here before, I find subscripted arrays to be clearer and >less error prone than incremented pointers (plus, vectorizing compilers >love finding those iteration variables.) (Having said that, I hope nobody >finds a bug in my loop.) Well, I don't immediately see any bugs in the loop. Agreed that incremented pointers are less clear than subscripted arrays, but they are usually more expensive, especially with older compilers. In this case, in order to explain the problem to the poster, you have to start talking about pointer/array equivalence, what duh[i] really means, etc. etc., and he's obviously not quite ready for that yet. I sent the poster a heavily commented version of the following, along with a blanket apology for the ridiculously large number of partially or completely incorrect answers to his very simple question. #include #include main() { char *duh = "Hello"; char *p; printf("%s\n", duh); p = duh; while (*p != '\0') { if (islower(*p)) *p = toupper(*p); p++; } printf("%s\n", duh); } Will