Path: utzoo!attcan!uunet!van-bc!ubc-cs!news-server.csri.toronto.edu!rutgers!usc!wuarchive!emory!att!pacbell.com!tandem!netcom!avery From: avery@netcom.UUCP (Avery Colter) Newsgroups: comp.lang.c Subject: Re: Is there a good example of how toupper() works? Message-ID: <15591@netcom.UUCP> Date: 26 Oct 90 07:15:32 GMT References: <2466@ux.acs.umn.edu> Organization: Netcom- The Bay Area's Public Access Unix System {408 241-9760 guest} Lines: 56 comp.lang.c/16831, edh@ux.acs.umn.edu > Basically, what I want to do is take a string of upper/lower case, and make > it all upper case. Here is a first try at it, > #include > main() > { > char *duh = "Hello"; > printf("%s\n", duh); > while (*duh <= strlen(duh)) { a) You should #include if you're going to use strlen. b) (*duh <= strlen (duh)) is a type mismatch. strlen returns an integer, while *duh is a character. And even with typecasting, as some others have said it is not a good condition. Better to do something like: while (*duh != NULL) Then you don't need to use #strlen or include . You can do this for one simple reason, at least with the compiler I use: the value strlen returns is the number of characters BEFORE THE TERMINATING NULL CHARACTER! > if (islower(*duh)) *duh = toupper(*duh); > *duh++; You gottit backwards, and dereferenced: ++duh; is what you want. > } > printf("%s\n", duh); > } > And what I get is : > Hello > Hello Small wonder: that while condition, even if it gets automatically typecasted, is comparing the ASCII value of 'H' to the length of the string, which is 5. Just make the while condition that the terminating NULL character of the string has been reached. -- Avery Ray Colter {apple|claris}!netcom!avery {decwrl|mips|sgi}!btr!elfcat (415) 839-4567 "Fat and steel: two mortal enemies locked in deadly combat." - "The Bending of the Bars", A. R. Colter