Is there a good example of how toupper() works?

Bill Poser poser at csli.Stanford.EDU
Wed Oct 17 05:53:23 AEST 1990


In article <2466 at ux.acs.umn.edu> edh at ux.acs.umn.edu (Eric D. Hendrickson) writes:
[believes that there is a problem with toupper and gives code including
the following]
>
>	char *duh = "Hello";
>	printf("%s\n", duh);
>	while (*duh <= strlen(duh)) {
>		if (islower(*duh)) *duh = toupper(*duh);
>		*duh++;
>	}

The problem here is in the while termination condition. What this tests
is whether the numerical value of the current character (*duh) is
less than or equal to the length of the the string duh, which happens always
to be five. This condition is never satisfied, so the code in the loop 
is never executed.

(An aside: since strlen(duh) never changes, either you or the compiler
should move it outside the loop.) 
							Bill



More information about the Comp.lang.c mailing list