Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!ccu.umanitoba.ca!salomon From: salomon@ccu.umanitoba.ca (Dan Salomon) Newsgroups: comp.lang.c Subject: Re: Is there a good example of how toupper() works? Message-ID: <1990Oct17.024233.16435@ccu.umanitoba.ca> Date: 17 Oct 90 02:42:33 GMT References: <2466@ux.acs.umn.edu> Organization: University of Manitoba, Winnipeg, Canada Lines: 48 In article <2466@ux.acs.umn.edu> edh@ux.acs.umn.edu (Eric D. Hendrickson) writes: >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)) { > if (islower(*duh)) *duh = toupper(*duh); > *duh++; > } > printf("%s\n", duh); >} There are at least four errors in this code. Two of them are in your while statement. There is no point in repeatedly recomputing the string length, and no point in comparing either a pointer, or the character it points to to that length. Instead test for the end of the string by finding the terminating null character. The other two errors, incrementing the character instead of the pointer, and trying to print the string by pointing to its end, were mentioned in earlier postings. Try the following version: #include main() { char *duh = "Hello"; char *cur; printf("%s\n", duh); cur = duh; while (*cur) { if (islower(*cur)) *cur = toupper(*cur); cur++; } printf("%s\n", duh); } Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debugging Monday's code. :-) -- Dan Salomon -- salomon@ccu.UManitoba.CA Dept. of Computer Science / University of Manitoba Winnipeg, Manitoba R3T 2N2 / (204) 275-6682