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

stripping non-numeric data from a string

P: n/a
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
ra*@oneconcept.net
+ 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.
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


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

Nov 14 '05 #2

P: n/a
In article <11*********************@f14g2000cwb.googlegroups. com>,
italy <it****@gmail.com> wrote:
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.
--
"I want to make sure [a user] can't get through ... an online
experience without hitting a Microsoft ad"
-- Steve Ballmer [Microsoft Chief Executive]
Nov 14 '05 #3

P: n/a
Raj <ra*********@thus.net> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

P: n/a
Raj
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> wrote in message
news:d5**********@canopus.cc.umanitoba.ca...
In article <11*********************@f14g2000cwb.googlegroups. com>,
italy <it****@gmail.com> wrote:
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.


I like that advice...fantastic.

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

Regards,
Raj
Nov 14 '05 #5

P: n/a
"Raj" <ra*********@thus.net> writes:
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.

--
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.
Nov 14 '05 #6

P: n/a

"Raj" <ra*********@thus.net> wrote

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.
Nov 14 '05 #7

P: n/a
Raj wrote:
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.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.