473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

stripping non-numeric data from a string

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
7 4841
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
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
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
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
"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

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

Similar topics

0
by: scott.watson | last post by:
Hello, I am attempting to attach a .pdf file to an email generated using oracle's utl_smtp. I am working on Oracle9i Enterprise Edition Release 9.2.0.4.0. I beleive that the...
1
by: Andy Jefferies | last post by:
I'm having problems stripping out the whitespace at the beginning of a particular element. In the XML snippet I've highlighted tabs and returns as ^I and ^M respectively: <para> ^I ^I ...
15
by: Jeff North | last post by:
Hi, I'm using a control called HTMLArea which allows a person to enter text and converts the format instructions to html tags. Most of my users know nothing about html so this is perfect for my...
8
by: John Buckley | last post by:
Hello! I have a program like this: #include <stdlib.h> int main(void) { /* do stuff */
258
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
4
by: Lance | last post by:
Hi, What way could I strip certain tags (like HTML comments) from the HTML being delivered to the client? I don't mean what regexp to use, but where do I put this stripping code? I'm thinking...
2
by: Peter Rooney | last post by:
hi all, i'm working on a project where i'm marshalling xml files to java objects created with JAXB 1.0 from an xsd schema. the problem is that the marshalling fails (UnexpectedElementException)...
6
by: Medros | last post by:
I understand that you can strip html out of a txt file so that all the information is left is the visable information that is needed (e.g. everything that has < > around is gone). My question is...
7
by: FFMG | last post by:
Hi, I have a form that allows users to comment, add entries and so on. But what a lot of them do is copy and paste directly from MS Word to my forms. almost all browsers will accept the post...
12
by: Stefan Arentz | last post by:
Is there a better way to do the following? attributes = attributeNames = {} n = 1 for attribute in attributes: attributeNames = attribute n = n + 1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.