Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comparing each of the string components

Reply
Thread Tools

Comparing each of the string components

 
 
Erwin
Guest
Posts: n/a
 
      08-22-2003
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.


 
Reply With Quote
 
 
 
 
foo bar
Guest
Posts: n/a
 
      08-22-2003
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.
>
>



 
Reply With Quote
 
 
 
 
Carl McGuire
Guest
Posts: n/a
 
      08-22-2003
>>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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-22-2003
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.

--

 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      08-22-2003
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

 
Reply With Quote
 
Erwin
Guest
Posts: n/a
 
      08-22-2003
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.
>
>



 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-22-2003
"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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing portion of string to another string Owner C Programming 2 02-18-2011 01:46 AM
Newbie XSL Question: Comparing to preceding-sibling within for-each Red XML 2 05-09-2007 01:44 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM
SWING components adjustment in different resolutions - Should show scrollbars less than 800X600 and expand components over this resolution Bluetears76 Java 1 07-01-2004 09:01 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57