Is there a good example of how toupper() works?
Dan Salomon
salomon at ccu.umanitoba.ca
Wed Oct 17 12:42:33 AEST 1990
In article <2466 at ux.acs.umn.edu> edh at 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 <ctype.h>
>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 <ctype.h>
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 at ccu.UManitoba.CA
Dept. of Computer Science / University of Manitoba
Winnipeg, Manitoba R3T 2N2 / (204) 275-6682
More information about the Comp.lang.c
mailing list