stripping non-numeric data from a string

Discussion in 'C Programming' started by Raj, May 10, 2005.

  1. Raj

    Raj Guest

    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

    + 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.
    Raj, May 10, 2005
    #1
    1. Advertising

  2. Raj

    italy Guest

    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.
    italy, May 10, 2005
    #2
    1. Advertising

  3. In article <>,
    italy <> 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]
    Walter Roberson, May 10, 2005
    #3
  4. Raj <> 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.
    Christopher Benson-Manica, May 10, 2005
    #4
  5. Raj

    Raj Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:d5qkjg$f8e$...
    > In article <>,
    > italy <> 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
    Raj, May 10, 2005
    #5
  6. "Raj" <> 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) <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.
    Keith Thompson, May 10, 2005
    #6
  7. Raj

    Malcolm Guest

    "Raj" <> 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.
    Malcolm, May 10, 2005
    #7
  8. Raj

    Joe Wright Guest

    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:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, May 11, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    378
  2. Replies:
    5
    Views:
    933
    X-Centric
    Jun 30, 2005
  3. darrel
    Replies:
    4
    Views:
    818
    darrel
    Jul 19, 2007
  4. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    963
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  5. Replies:
    5
    Views:
    312
Loading...

Share This Page