Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!orc!inews!cmdnfs!bhoughto From: bhoughto@cmdnfs.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: Is there a good example of how toupper() works? Message-ID: <473@inews.intel.com> Date: 16 Oct 90 21:59:16 GMT References: <2466@ux.acs.umn.edu> <15857@csli.Stanford.EDU> Sender: news@inews.intel.com Organization: Intel Corp, Chandler, AZ Lines: 40 Sorry if anyone saw my earlier posting to this thread; a) I thought I was mailing it; b) I thought I had hit 'l' for 'list' instead of 's' for 'send': and there were several things yet to edit. I went after ^C, but it was too late... the cancellation should be getting through any time now... In article <15857@csli.Stanford.EDU> poser@csli.stanford.edu (Bill Poser) writes: >In article <2466@ux.acs.umn.edu> edh@ux.acs.umn.edu (Eric D. Hendrickson) writes: >> char *duh = "Hello"; >> printf("%s\n", duh); >> while (*duh <= strlen(duh)) { >> if (islower(*duh)) *duh = toupper(*duh); >> *duh++; >> } >> printf("%s\n",duh) > >The problem here is in the while termination condition. What this tests There's more than just the one problem (that *duh will be > strlen(duh)); 0. *duh refers to the character, not the location; 1. The loop changes the value of the pointer `duh', so it may print nothing other than "O" once you get the loop to work; 2. merely using (duh <= strlen(duh)) won't fix it; the value of the pointer `duh' is almost certain to be larger than strlen(duh). I won't give fixes here; it's too instructive to work them out yourself, especially at the apparent level of understanding. >(An aside: since strlen(duh) never changes, either you or the compiler >should move it outside the loop.) Trivial optimization compared to the massive bugs extant. --Blair "I've been saying 'duh' myself a lot, lately..."