string manipulations
Author Message
string manipulations

Greetings,
Can anyone tell me how to separate a number from a string?
E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
trivial...
Thanks
Jeroen
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:

> Greetings,
> Can anyone tell me how to separate a number from a string?
> E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
> trivial..

If you know where the number is every time then try something along the
lines of:

char    *str    = "abc 12.3 cde";
double  d = atof( str + 4 );

atof will stop converting when it hits the "space" following the
3.

If you DON'T know where the number is then you might try several calls
to strtok():

char    *str    = "abc 12.3 cde";
char    *tok;
char    *endp;
double  d;
tok = strtok( str, ' ' );
while (1) {

/* try to convert the token */
d = strtod( tok, &endp );

/* if the token was not a number then endp will
still point to the beginning of the current
token in which case we need to get the next
token. */
if ( endp == tok ) {
tok = strtok( NULL, ' ' );
} else {
break;
}
};

Of course, in *this* code if there IS NO number then you'll be waiting
for a long time :)

Hope that helps. I wouldn't be surprised if there's something more
"elegant". Anyone?

Quote:
> Thanks
> Jeroen
> --

--
Aaron Jewell
Jr. Software Engineer
Willowglen Systems Inc.
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:

> Can anyone tell me how to separate a number from a string?
> E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
> trivial...

Find the first digit using strcspn(); convert the following number using
strtod(), which will not get confused if the number is followed by a
space.
If you need the string representation, find the first digit using
strcspn(), then find the first non-digit from after that using strspn(),
then apply strncpy() or strncat().

Richard
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:
> Greetings,
> Can anyone tell me how to separate a number from a string?
> E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
> trivial...

If you don't mind corruption of the original string:

{
char *str = "abc 12.3 cde";
char *numStr;

while (*str && !isdigit(*str))    /* skip non-digits (don't accept .23) */
str++;
numStr = str;
while (*str && (isdigit(*str) || (*str == '.')))    /* check for digit and
DOT */
str++;
*str = 0;
printf("The numberstring is '%s'\n",numStr);

Quote:
}

If you know that there is a SPACE character, you could also use strchr(str,'
'), but I don't expect that to be easier.

Hope this helps,

Pieter
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:

> Greetings,
> Can anyone tell me how to separate a number from a string?
> E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
> trivial...

Sorry.. on second read I realize that the original answer I gave you is

But the solution is similar..

If you know where the number begins:

char *num;
char *str = "abc 12.3 cde";
num = strtok( str + 4, ' ' );

Be aware that this effectively "chops" your originial string apart by
putting a '\0' right after the 3 - but num will point to a null
terminated string containing your number. Obviously, if you don't
want the original string ripped apart like this then you can
always just make a copy of it to work with.

If you don't know where the number begins then then you can still use
several calls to strtok() and test the initial character of the returned
string with isdigit(). If you get a positive return from isdigit() then
you know you've found what is likely the number you want.

char *num;
char *str = "abc 12.3 cde";
num = strtok( str, ' ' );
while ( num != NULL && !isdigit(*num) ) {
num = strtok( NULL, ' ' );
}
if ( num == NULL ) { you don't have what you want. }
else { party on! }

I Hope that's better.
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:
> Greetings,
> Can anyone tell me how to separate a number from a string?
> E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
> trivial...

It's trivial anyway:
char * YourString =  "whatever with digits 123 test";

char * WorkPtr = YourString;
while( *WorkPtr != '\0' && ! isdigit( *WorkPtr ) )
WorkPtr++;

Assuming I did not make a stupid mistake -- I haven't  used up my daily
quota for that _completely_ ;-) -- WorkPtr should afterwards point to the
first digit in YourString or to the end of it.

--
Greetings from
_____
/_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
Remove SPAMBLOCK from my address ...
--

Mon, 10 Feb 2003 03:00:00 GMT
string manipulations

Quote:
>Greetings,
>Can anyone tell me how to separate a number from a string?
>E.g., how do I reduce "abc 12.3 cde" to "12.3"? From there on, it's
>trivial...

With the following assumptions:

1. This is a line taken from a file with fgets or a similar function of

2. The line consists of three fields, the first of characters, the
second of digits (+ decimal point) the third of characters.

3. The fields are separated by spaces and the character fields do NOT
have internal spaces so the following string is not valid
"abc def 12.3 ghi"

#include <string.h>

#define MAX_LEN  50

size_t len = strlen(input_string);
size_t i = 0;
char  *p = input_string;
char field_1[MAX_LEN];
char field_2[MAX_LEN];
char field_3[MAX_LEN];

for( i = 0; *p != ' '&& i < len; ++i)
{
field_1[i] = *p++;

Quote:
}

field_1[i] = '\0';
p++;

for( i = 0; *p != ' '&& i < len; ++i)
{
field_2[i] = *p++;

Quote:
}

field_2[i] = '\0';
p++;
/* Use strtod on this to get the number as a double */

for( i = 0; *p != ' '&& i < len; ++i)
{
field_3[i] = *p++;

Quote:
}

field_3[i] = '\0';
--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--

Mon, 10 Feb 2003 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages