By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

Extract the integer value from a string (beginner)

P: n/a
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.

Sep 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11*********************@e3g2000cwe.googlegroups.c om>,
fool <as******@yahoo.co.ukwrote:
>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.
> val = atoi(data);
printf("%d\n",val);

}

return 0;
}
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 20 '06 #2

P: n/a

fool wrote:
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??
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.
Sep 20 '06 #3

P: n/a
fool wrote:
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.

--
Ian Collins.
Sep 20 '06 #4

P: n/a
In article <11**********************@b28g2000cwb.googlegroups .com>,
va***************@gmail.com says...
>
fool wrote:
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?
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.
>
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.

Sep 20 '06 #5

P: n/a
fool wrote:
In article <11**********************@b28g2000cwb.googlegroups .com>,
va***************@gmail.com says...
>fool wrote:
>>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?

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) {
...
}
Sep 20 '06 #6

P: n/a
fool <sa***********@REMOVEgmail.comwrites:
In article <11**********************@b28g2000cwb.googlegroups .com>,
va***************@gmail.com says...
>>
fool wrote:
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?

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.
>>
if(data)
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);".
scanf("%s",data);
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 20 '06 #7

P: n/a
On Wed, 20 Sep 2006 11:02:50 +0530, in comp.lang.c , fool
<sa***********@REMOVEgmail.comwrote:
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
char tmp = data[0-9];
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
Sep 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.