473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing each of the string components

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*)p oint)
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.
Nov 13 '05 #1
6 3576
You forgot to use the indirection operator (*):
Some corrections:
while (*pointer)

isalpha(*pointe r)

isdigit(*point)

"Erwin" <er********@yah oo.com.sg> wrote in message
news:Mk******** ***********@new s-server.bigpond. net.au...
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*)p oint)
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.

Nov 13 '05 #2
>>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(*pointe r)

isdigit(*point)

"Erwin" <er********@yah oo.com.sg> wrote in message
news:Mk******** ***********@new s-server.bigpond. net.au...


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

Nov 13 '05 #3
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*)p oint)
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 misunderstandin gs 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((unsig ned 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.

--
Er*********@sun .com
Nov 13 '05 #4
On Fri, 22 Aug 2003 15:07:24 GMT, "Erwin" <er********@yah oo.com.sg>
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
re************* ***********@att .net
Nov 13 '05 #5
One last question about isdigit(*pointe r)

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" <er********@yah oo.com.sg> wrote in message
news:Mk******** ***********@new s-server.bigpond. net.au...
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*)p oint)
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.

Nov 13 '05 #6
"Erwin" <er********@yah oo.com.sg> wrote:
One last question about isdigit(*pointe r)

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.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2253
by: AJ | last post by:
Hi All Just a quickie. A form element for a search will contain either a town or a post code. Is there any way we can check the input to see if the element contains a number (in which case it will be a postcode) or not (in which case it will be a town). Cheers Andy
0
1092
by: Dan Noel | last post by:
Our company has developed a framework for working with complex .net assemblies. We had hoped to develop a flexible "component" environment that would allow us to swap components in and out without a great deal of depedencies. In reality, we are finding the subtle changes in interfaces and libraries break things like serialization. But, these small changes are hard to track and detect.
5
2428
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it: while( ( custinfo.number >...
41
3971
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course to iterate throug the lists and compare each of the element, but is there a more efficient way? ...
88
22085
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm wondering if one is better than the other or if there is some sort of hybrid of the two that would be superior. IMPLEMENTATION 1: #ifndef HAVE_STRCASECMP
1
11261
by: Jeffrey B. Holtz | last post by:
I'm trying to get the Name of the USB device pluged in from the RegisterDeviceNotification that I've used P/Invoke to marshal. I have seen a similar posting on the VisualBasic newgroups but I do not know how to translate the ReDim that occurs there into C#. Or wither this will actually give me what I'm looking for. The code I'm posting seems to work although I don't know how to set the length of the name correctly in the...
5
3809
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one of the values comes from the database, and for existing values it works fine, but if the date doesn't exist (which will always be the condition when the user first enters into the form) I am adding logic to my javascript like: if (dbDate <...
2
3390
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
17
8649
by: junky_fellow | last post by:
Guys, Is it a good way to compare two structures for equality by using "memcmp" ? If not, what could be the problems associated with this ? And what is the correct method of doing this ? thanks for any help.
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.