Erwin said:
Suppose I have a string, char *str = "1 2 a f g < )"
and I need to check each of the components in the loop using isalpha,
isdigit, isspace. How can I do that?
I tried to do using this code, but gives me a core dump.
It should not even have compiled. I'm going to guess
that you failed to #include <ctype.h>, which would explain
why the compiler didn't complain about the faulty code. In
the future, though, don't make us guess: Post a minimial
complete program that demonstrates the problem, not an
out-of-context snippet that requires us to imagine what
you may have done, and then waste time trying to explain
the error we've imagined that you made instead of the
error you actually made.
"Doctor, it hurts!"
"Where does it hurt?"
"Here's a picture of a one-inch square chunk of skin
somewhere near the painful point, or perhaps somewhere
far removed from it. Cure me!"
char *point;
point = str;
while (point != '\0') {
if (isalpha(point)) // It does not work either: if
(isalpha(int*)point)
printf("The string contains a character");
else if (isdigit(point))
printf("The string cannot contain a number");
exit(1);
point++;
}
Okay, first things first: You should #include <stdio.h>
because you're using printf(), one of the functions declared
there. You should #include <stdlib.h> because you're using
exit(). And you should #include <ctype.h> because you're
using the isxxxx() functions. As you've (perhaps) learned,
omitting the required inclusions may keep the compiler from
complaining -- but that doesn't mean there's nothing to
complain about. Another side-effect is that even when there's
nothing to complain about, omitting the inclusions may keep
the compiler from generating the correct code.
Second, I'm just going to sort of imagine there's an
actual, executable function context here somewhere, and that
`str' is as you've described (can't be sure of that, since
you haven't shown where it comes from -- and, given some of
the other misunderstandings evident in what you *have* shown,
it's not at all certain you got it right).
Third, the isxxxx() functions operate on a single character,
not upon an entire string of characters. You can't use them
to ask "Does this string contain a digit?" You *can* examine
the string's characters one by one and ask "Is this single
character a digit? How about this other character? And
this one?"
Fourth, a subtlety you can be forgiven for overlooking:
it's a clumsiness in the way C and its library are defined,
and the result is that you need to write counter-intuitive
code that no reasonable language would require. Instead of
the obvious
if (isalpha(*point)) /* wrong! */
to test whether the character pointed to by `point' is or
isn't alphabetic, you must write
if (isalpha((unsigned char)*point)) /* ugly! */
instead, and similarly for the other isxxxx() functions and
for toupper() and tolower(). The reasons, I think, would
only confuse you at your present state of development: for
now, Just Do It. When you've attained more security in the
language and its concepts, you can explore the whys and
wherefores, and convert magical incantations into actual
understanding. But for now, just throw a pinch of salt
over your left shoulder, spit at the moon, turn 'round
three times widdershins, and intone `(unsigned char)'.
Finally, exit(1) is a perfectly legal way to terminate
a program -- but the meaning of the `1' may be different
on different computers. On many that I've used, `1' means
failure -- but on another, `1' means success! You're free
to use `1' if you like, but there are portable alternatives:
- `0' means success
- `EXIT_SUCCESS' also means success, and may perhaps
mean a different "flavor" of success
- `EXIT_FAILURE' means failure
The EXIT_xxx values are defined in <stdlib.h> -- which
you should #include if you're going to use exit(), so they're
available. These same three values also have the same
meanings if the main() function returns them.