Comparing each of the string components

Discussion in 'C Programming' started by Erwin, Aug 22, 2003.

  1. Erwin

    Erwin Guest

    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.
     
    Erwin, Aug 22, 2003
    #1
    1. Advertising

  2. Erwin

    foo bar Guest

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

    isalpha(*pointer)

    isdigit(*point)

    "Erwin" <> wrote in message
    news:Mkq1b.52215$...
    > 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.
    >
    >
     
    foo bar, Aug 22, 2003
    #2
    1. Advertising

  3. Erwin

    Carl McGuire Guest

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

    >
    > foo bar wrote:
    > You forgot to use the indirection operator (*):
    > Some corrections:
    > while (*pointer)
    >
    > isalpha(*pointer)
    >
    > isdigit(*point)
    >
    > "Erwin" <> wrote in message
    > news:Mkq1b.52215$...
    >


    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
     
    Carl McGuire, Aug 22, 2003
    #3
  4. Erwin

    Eric Sosman Guest

    Erwin wrote:
    >
    > 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.

    --
     
    Eric Sosman, Aug 22, 2003
    #4
  5. Erwin

    Alan Balmer Guest

    On Fri, 22 Aug 2003 15:07:24 GMT, "Erwin" <>
    wrote:

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


    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 22, 2003
    #5
  6. Erwin

    Erwin Guest

    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.


    "Erwin" <> wrote in message
    news:Mkq1b.52215$...
    > 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.
    >
    >
     
    Erwin, Aug 22, 2003
    #6
  7. Erwin

    Simon Biber Guest

    "Erwin" <> wrote:
    > 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.

    --
    Simon.
     
    Simon Biber, Aug 22, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    420
    Eliyahu Goldin
    Aug 3, 2005
  2. Mickey Segal
    Replies:
    0
    Views:
    913
    Mickey Segal
    Feb 2, 2004
  3. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    440
    Tjerk Wolterink
    Nov 3, 2004
  4. Red
    Replies:
    2
    Views:
    1,226
    Pavel Lepin
    May 9, 2007
  5. Pat Maddox
    Replies:
    6
    Views:
    160
    Marcin Mielżyński
    Jan 20, 2006
Loading...

Share This Page