473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Extract the integer value from a string (beginner)

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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
3
by: Lauren Quantrell | last post by:
This a a long convoluted string parsing question... I have a string in an Access 2K database table field that I use for noting if a user has checked a record. The string goes like this: xy So...
6
by: Mohammad-Reza | last post by:
Hi I want to extract icon of an exe file and want to know how. I look at the MSDN and find out that I can use ExtractIconEx() Windows API but in there are some changes to that api in c# I made...
1
by: Patrick Sullivan | last post by:
I am trying to extract two parts of a number from an array element. Numbers are in the format of 1.10, 2.50, 11.10, etc. Floor and ceiling won't work right because close to 1.00, I get a zero, and...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
6
by: Ben | last post by:
Hi We have a Dataset that has been populated from the output parameter of a Stored Procedure (@Output). I understand that I can extract a single item when the dataset is populated by a table...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
19
by: stinkinrich88 | last post by:
Hello! I'm really new to c++. I've been doing java for a while though. I'm using linux so I wanted to make a gtk+ gui application. I want to make a simple calculator to help me learn, so, say...
2
by: clai83 | last post by:
mysql and mysqli functions always return strings values, and I understand that I can set the type of the data via the settype function AFTER I extract the data, but is there a way with PHP to extract...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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 ...
0
muto222
php
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.