Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

who can I find a integer string in a longer string?

Status
Not open for further replies.

sidalla

Programmer
Oct 5, 2000
2
AU
hi there,
deos anyone know how to extract a integer from a string.

Say I use the srtsrt(s1, s2) to search for s2 in s1. What if I want to check if s1 has an interger?

Thanks for any help. [sig][/sig]
 
You can not use a String function to treat integers.
First you have to convert the integer to a String:
sprintf(charvar,&quot;%d&quot;,integervar); [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Thank hnd,
The sub string (int string) I want to find in the string is not predefined. I want
to check if there ANY interger in the main string. Is there a way predefined method for this, of I have to do it my self.

Thanks in advance for you time.
[sig][/sig]
 
I think you have to scan your STring for integer substrings by yourself.

But this is pretty simple.

If is definite, that the integer begins on a well defined location of your String than you can use tha atoi function to convert this String. [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
There is no predefined method for this. You will need to break the string down into individual words and then check each word to see if it id an integer.

It is pretty simple to do actually. If you want I can post some code later on that will do it.

Right now, somebody owes me a beer and Im going to collect. Heh.

Regards,
Gerald
[sig][/sig]
 
I think sidalla's problem is to :
1) find if there is any integer in the given string.
2) If yes then convert it to integer value:

If I am right then,

Do this way.

1) Scan the string for integer's Ascii value (ie, 0-9). If found then find how big is the integer. If it can fit into the computer's integer limit. Then,

2) get the substring out of that .

And convert it to integer by any method : like
atoi, sprintf..

Does this answer your question?
Thanks
Siddhartha Singh [sig]<p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br> [/sig]
 
Hi,

To expand a little on what has been said, I think the OP is looking for a function that scans through an arbitrary length string for one or more sequences of numbers. For each sequence found, convert the sequence to an integer.

If this is close to what you want, below is something that might help you get started. Please keep in mind that this has NOT been compiled and tested (no C compiler on this comp :), so there may be errors. This could certainly be improved to provide better functionality (maybe store the numbers in a user-supplied array instead of just printing the values) and it lacks bounds checking (to make sure the number is suitable for an int).

#include <stdio.h>
#include <string.h>
#include <math.h>

#define INTEGER_OUT 0
#define INTEGER_IN 1

void integers_in_string(const char s[])
{
int state,pwr,num,i;

for (i=strlen(s)-1,state=INTEGER_OUT,pwr=num=0;i>=0;--i) {
if (s>='0' && s<='9') {
state=INTEGER_IN;
num+=(int) (s * pow(10,pwr));
++pwr;
if (i==0) {
printf(&quot;found %d\n&quot;,num);
}
} else {
if (state==INTEGER_IN) {
printf(&quot;found %d\n&quot;,num);
cur_num=power=0;
state=INTEGER_OUT;
}
}
}
} [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top