stripping non-numeric data from a string

R

Raj

Hi

I was hoping someone could suggest a simple way of stripping non-numeric
data from a string of numbers.

For example, if I have "ADB12458789\n"

I would like to remove the letters and the newline from this string.

I am new to C so am sure this is simple ut I don't know how to do it! Sorry!
Do you have to tokenise the string?

Thanks for any help anyone can give.

Regards,
--
Raj Kothary :: one|concept
http://www.oneconcept.net
(e-mail address removed)
+ 44 (0)79 5647 2746

oneconcept limited :: 17 York Avenue, Stanmore, Middlesex HA7 2HT

Confidentiality notice:
The information transmitted in this email and/or any attached document(s) is
confidential and intended only for the person or entity to which it is
addressed and may contain privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon this
information by persons or entities other than the intended recipient is
prohibited. If you received this in error, please contact the sender and
delete the material from any computer.
 
I

italy

Hi there.

I recommend writing a function to do this. Write a function to look for
letters (you can iterate using the standard function isalpha()). If the
loop finds a letter, remove it. Continue the loop until '\n' is
encountered. Once '\n' is found, remove it and break the loop.
 
W

Walter Roberson

I recommend writing a function to do this.

"This" ?? You didn't quote any context :(
Write a function to look for
letters (you can iterate using the standard function isalpha()). If the
loop finds a letter, remove it.

The OP asked how to remove non-numeric characters. That's !isdigit()
rather than isalpha(). If you use isalpha() as the test, you would
leave in whitespace and special characters.

Like they say in Perl discussions: Don't parse to eliminate what
you don't want -- parse to retain what you do want. Otherwise
some new or special case that you didn't consider will come along and
bite you.
 
C

Christopher Benson-Manica

Raj said:
For example, if I have "ADB12458789\n"
I would like to remove the letters and the newline from this string.
I am new to C so am sure this is simple ut I don't know how to do it! Sorry!
Do you have to tokenise the string?

No, you do not have to tokenize the string. Use the standard library
function isdigit() to determine which characters to keep; move those
characters to the beginning of the string. Give that a shot.
 
R

Raj

Walter Roberson said:
"This" ?? You didn't quote any context :(


The OP asked how to remove non-numeric characters. That's !isdigit()
rather than isalpha(). If you use isalpha() as the test, you would
leave in whitespace and special characters.

Like they say in Perl discussions: Don't parse to eliminate what
you don't want -- parse to retain what you do want. Otherwise
some new or special case that you didn't consider will come along and
bite you.

I like that advice...fantastic.

Thanks also to italy and Christopher for your replies. I will try this
approach.

Regards,
Raj
 
K

Keith Thompson

Raj said:
I was hoping someone could suggest a simple way of stripping non-numeric
data from a string of numbers.

For example, if I have "ADB12458789\n"

I would like to remove the letters and the newline from this string.

What do you want to do if the non-digits are embedded, as in
"123ABC456" or "1A2B3C4"? I'm guessing you want "123456" and "1234",
respectively, but depending on what problem you're solving you might
want to treat them as errors or as multiple distinct sequences.

[...]
Confidentiality notice:
The information transmitted in this email and/or any attached document(s) is
confidential and intended only for the person or entity to which it is
[...]

See if you can find a way to avoid posting this notice. It makes no
sense for an article posted to Usenet.
 
M

Malcolm

Raj said:
For example, if I have "ADB12458789\n"

I would like to remove the letters and the newline from this string.
Depends exactly what you want to do.

What I would do is call strlen(), subtract one, and look at the last
character in the string. If it is a newline, repace it with a NUL (Only you
know what to do if it is not a newline).
Then work backwards, calling isdigit(), until you hit either the start of
the string or a non-digit.
(Only you know what happens if the character before the newline is not a
digit, or if decimal points and minus signs are allowed).

Your pointer now starts to the beginning of the number, which is
NUL-terminated. Copy to a temporary buffer with strcpy(), and then copy from
the temporary to the oriignal buffer with another call to strcpy(). You
could make this more efficient but that would be over-optimisation.
 
J

Joe Wright

Raj said:
Hi

I was hoping someone could suggest a simple way of stripping non-numeric
data from a string of numbers.

For example, if I have "ADB12458789\n"

I would like to remove the letters and the newline from this string.

I am new to C so am sure this is simple ut I don't know how to do it! Sorry!
Do you have to tokenise the string?

Thanks for any help anyone can give.

Regards,

/* Remove all but 'digits' from a string */

#include <stdio.h>
#include <ctype.h>

void digs(unsigned char *s) {
unsigned char c, *d = s;
while ((c = *s++))
if (isdigit(c))
*d++ = c;
*d = 0;
}

int main(void) {
char line[] = "ADB12458789\n";
fputs(line, stdout);
digs(line);
puts(line);
return 0;
}

No comments here Raj, this is meant to be a lesson in reading C.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top