Comparing each of the string components

E

Erwin

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.

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++;
}

....
....

Thank you for the inputs.
Cheers.
 
F

foo bar

You forgot to use the indirection operator (*):
Some corrections:
while (*pointer)

isalpha(*pointer)

isdigit(*point)
 
C

Carl McGuire

Suppose I have a string, char *str = "1 2 a f g < )"
> foo bar wrote:
> You forgot to use the indirection operator (*):
> Some corrections:
> while (*pointer)
>
> isalpha(*pointer)
>
> isdigit(*point)
>
> >

Top posting fixed.

In addition, the call to exit is made after the if statement no matter
what. You need to open an open curly "{" after the else and a close
curly after the call to exit, then fix your indenting too.

Carl
 
E

Eric Sosman

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.
 
A

Alan Balmer

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.

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++;
}
Since point is a pointer, not a char, isalpha and isdigit have no
opinion on it :)

You need to dereference the pointer to get the character it's pointing
to, as in

if (isalpha( *point))
....
 
E

Erwin

One last question about isdigit(*pointer)

Since I have defined pointer as:
char *pointer.

and based on the definition of isdigit: isdigit(c), where c is int c, and I
have defined char *pointer
after dereferencing pointer, why can I still get the isdigit function to
work properly, since after dereferencing it, pointer will return char,
instead of int, as per definition.

Thanks for clarification.
 
S

Simon Biber

Erwin said:
One last question about isdigit(*pointer)

Since I have defined pointer as:
char *pointer.

and based on the definition of isdigit: isdigit(c), where c is
int c, and I have defined char *pointer after dereferencing
pointer, why can I still get the isdigit function to work
properly, since after dereferencing it, pointer will return
char, instead of int, as per definition.

Somewhere in the <ctypes.h> that you included will be code like:
int isdigit(int C);

This line tells the compiler that the isdigit function takes one
parameter of int type. After that, whenever you call the isdigit
function, the compiler will automatically attempt a conversion
from the supplied type (char) to the function's actual argument
type (int). For char and int, this should be a lossless
conversion.

Actually, as Eric pointed out, you should include an explicit
conversion on the argument, but not to the target type (int),
rather to (unsigned char). The compiler will still do the
implicit conversion from (unsigned char) back to (int).

This has the result of ensuring that the input to the is* and
to* functions will be positive, and can safely be used as an
array index, in case the functions are implemented that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top