Extract the integer value from a string (beginner)

F

fool

Dear group,
Extract the integer value present in a given string. So I tried the
following:

int main(void)
{
int val;
char *data;
data = malloc(sizeof *data);
if(data)
scanf("%s",data);
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];
if(*data == tmp)
val = atoi(data);
printf("%d\n",val);

}

return 0;
}

It does not print what I wanted. can any one guide me to the solution.
please don't write the program for me. Thanks.
 
W

Walter Roberson

fool said:
int main(void)
{
int val;
char *data;
data = malloc(sizeof *data);

All you are malloc()'ing there is enough for *one* character.
if(data)
scanf("%s",data);

and there you use scanf to read an indefinite number of characters
and a terminating '\0' that takes up a character itself. So you
are going to have buffer overflow for sure.
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];

No, that tries to access the negative-9th element of the array 'data'.
if(*data == tmp)

It looks like you are trying to pattern match there. C does not
offer a pattern match operator.
 
V

valis.eric.ykchan

fool said:
Dear group,
Extract the integer value present in a given string. So I tried the
following:

int main(void)
{
int val;
char *data;
data = malloc(sizeof *data);

How large are you allocating?
if(data)
scanf("%s",data);
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];

What are you doing here?
tmp is 0, 1,.. or 9?
tmp only has 1 value
if tmp is 0, and *data is 9, what do you expect??
 
I

Ian Collins

fool said:
Dear group,
Extract the integer value present in a given string. So I tried the
following:

int main(void)
{
int val;
char *data;
data = malloc(sizeof *data);
if(data)
scanf("%s",data);
This isn't a good idea, you only allocate one byte.

Keep is simple and use a reasonably large automatic buffer.

It is safer to use fgets to read the string as you can limit the number
of characters read to the size of your buffer.
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];

No, this attempts to assign data[-9] to tmp. You have to test each
character.
if(*data == tmp)
val = atoi(data);

Prefer strtol over atoi, it gives you error reporting.
printf("%d\n",val);

}

return 0;
}

It does not print what I wanted. can any one guide me to the solution.
please don't write the program for me. Thanks.
I hope the above comments help.
 
F

fool

How large are you allocating?

data is de-refered, so memmory size is that of the whole string inputed,
correct?
if(data)
scanf("%s",data);
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];

What are you doing here?
tmp is 0, 1,.. or 9?
tmp only has 1 value
if tmp is 0, and *data is 9, what do you expect??


I want the tmp to hold only the integral values from a given string.
Like extract and assign any value from 1 - 9. It does not work.
 
?

=?ISO-8859-15?Q?=22Nils_O=2E_Sel=E5sdal=22?=

fool said:
data is de-refered, so memmory size is that of the whole string inputed,
correct?
At this point, no data is even read, so how long should it be ?
anyway, sizeof expression gives the size of the type of the expression
The type of *data is char, and sizeof(char) is 1.
This is equivialent to data = malloc(sizeof(char)); or data = malloc(1);
You need room for more than 1 character.

As an alternative,you can read the scanf documentation, and ponder over:
int i;
if(scanf("%d",&1)) == 1) {
...
}
 
K

Keith Thompson

fool said:
data is de-refered, so memmory size is that of the whole string inputed,
correct?

Nope.

data is of type char*, so *data is of type char, and "sizeof *data" is
the size of a single character (i.e., 1).

malloc() attempts to allocate however many bytes you ask it to. Since
you haven't performed the input yet, you have no way of knowing how
long it is, and therefore how many bytes you'll need to allocate. But
until you do the allocation, you have no place to put the bytes you're
reading. It seems like a vicious circle; the simplest solution is to
estimate how many bytes you'll need, and be careful not to overflow
the allocated space.

You check whether malloc() succeeded, which is great -- but you need
to decide what to do if it failed. For a simple program like this, it
probably makes sense just to bail out: "exit(EXIT_FAILURE);".

You don't want to use "%s" with scanf(). It reads a potentially
unlimited number of characters; there's no way you can allocate enough
space to hold them all. It also reads, not an arbitrary string, but a
word delimited by white space; that's probably not what you want
either.
while((*data++) != '\0')
{
/* Only the ascii char 0-9 is assigned to tmp (correct me if I am
wrong)*/
char tmp = data[0-9];
What are you doing here?
tmp is 0, 1,.. or 9?
tmp only has 1 value
if tmp is 0, and *data is 9, what do you expect??

Sorry, that doens't make much sense.

C doesn't have a range operator. In "0-9", the "-" is just a
subtraction operator; the result is negative 9, which definitely isn't
what you want here.

You want to look at just the characters that are digits, right? C
doesn't give you a direct way to express that kind of thing. What you
*can* do is:

for each character
if it's a digit
do something
otherwise
do something else

You'll need to decide what "something else" is. (Error handling can
be tricky; often deciding what to do if you encounter an error is
harder than detecting the error in the first place.)
 
M

Mark McIntyre

data is de-refered, so memmory size is that of the whole string inputed,
correct?

No.
Data is a pointer to a single char. You need to allocate more than
that, if you want to copy more in.

data = malloc(99 * sizeof *data); // allocates space for 99 chars

This accesses the minus ninth entry in the array 'data'. Its not what
you want to do.
I want the tmp to hold only the integral values from a given string.

Then you have to examine each character of the string in turn, and
decide if it is the character representation of an integer. A for loop
would be useful for this, and remember that the characters '0' to '9'
appear in order in the character set.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top