Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stripping non-numeric data from a string

Reply
Thread Tools

stripping non-numeric data from a string

 
 
Raj
Guest
Posts: n/a
 
      05-10-2005
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
http://www.velocityreviews.com/forums/(E-Mail 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.


 
Reply With Quote
 
 
 
 
italy
Guest
Posts: n/a
 
      05-10-2005
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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-10-2005
In article <(E-Mail Removed). com>,
italy <(E-Mail Removed)> 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]
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-10-2005
Raj <(E-Mail Removed)> 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.
 
Reply With Quote
 
Raj
Guest
Posts: n/a
 
      05-10-2005
"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
news:d5qkjg$f8e$(E-Mail Removed)...
> In article <(E-Mail Removed). com>,
> italy <(E-Mail Removed)> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-10-2005
"Raj" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      05-10-2005

"Raj" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-11-2005
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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stripping unwanted chars from string Edward Elliott Python 7 05-04-2006 03:05 PM
string stripping issues orangeDinosaur Python 7 03-03-2006 04:08 PM
string.lstrip stripping too much? joram gemma Python 4 05-16-2005 07:23 AM
Re: stripping a string Jeff Epler Python 4 09-16-2003 12:09 PM
stripping a string Leeds, Mark Python 1 09-16-2003 09:56 AM



Advertisments