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

Will Crowder will at kfw.COM
Thu Oct 18 02:55:09 AEST 1990


In article <1990Oct16.221035.10764 at nntp-server.caltech.edu> bruce at 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 <ctype.h>
>main() {
>        char *duh = "Hello";
>        int i, limit = strlen(duh);
>        printf("%s\n", duh);
>        for(i=0; i<limit; 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 <stdio.h>
#include <ctype.h>

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



More information about the Comp.lang.c mailing list