stripping non-numeric data from a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raj

    stripping non-numeric data from a string

    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 "ADB1245878 9\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
    ONE Concept Group is Canada's leading conference both on line and in person for Massage Therapists and Integrative Health Care Professionals.

    raj@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.


  • italy

    #2
    Re: stripping non-numeric data from a string

    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.

    Comment

    • Walter Roberson

      #3
      Re: stripping non-numeric data from a string

      In article <1115739133.565 000.77740@f14g2 000cwb.googlegr oups.com>,
      italy <italys@gmail.c om> wrote:[color=blue]
      >I recommend writing a function to do this.[/color]

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

      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]

      Comment

      • Christopher Benson-Manica

        #4
        Re: stripping non-numeric data from a string

        Raj <raj.kothary@th us.net> wrote:
        [color=blue]
        > For example, if I have "ADB1245878 9\n"[/color]
        [color=blue]
        > I would like to remove the letters and the newline from this string.[/color]
        [color=blue]
        > 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?[/color]

        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)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Raj

          #5
          Re: stripping non-numeric data from a string

          "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.ca> wrote in message
          news:d5qkjg$f8e $1@canopus.cc.u manitoba.ca...[color=blue]
          > In article <1115739133.565 000.77740@f14g2 000cwb.googlegr oups.com>,
          > italy <italys@gmail.c om> wrote:[color=green]
          >>I recommend writing a function to do this.[/color]
          >
          > "This" ?? You didn't quote any context :(
          >[color=green]
          >>Write a function to look for
          >>letters (you can iterate using the standard function isalpha()). If the
          >>loop finds a letter, remove it.[/color]
          >
          > 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.[/color]

          I like that advice...fantas tic.

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

          Regards,
          Raj


          Comment

          • Keith Thompson

            #6
            Re: stripping non-numeric data from a string

            "Raj" <raj.kothary@th us.net> writes:[color=blue]
            > I was hoping someone could suggest a simple way of stripping non-numeric
            > data from a string of numbers.
            >
            > For example, if I have "ADB1245878 9\n"
            >
            > I would like to remove the letters and the newline from this string.[/color]

            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.

            [...][color=blue]
            > 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[/color]
            [...]

            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_Keit h) kst-u@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.

            Comment

            • Malcolm

              #7
              Re: stripping non-numeric data from a string


              "Raj" <raj.kothary@th us.net> wrote[color=blue]
              >
              > For example, if I have "ADB1245878 9\n"
              >
              > I would like to remove the letters and the newline from this string.
              >[/color]
              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.


              Comment

              • Joe Wright

                #8
                Re: stripping non-numeric data from a string

                Raj wrote:[color=blue]
                > 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 "ADB1245878 9\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,[/color]

                /* 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:joewwrig ht@comcast.net
                "Everything should be made as simple as possible, but not simpler."
                --- Albert Einstein ---

                Comment

                Working...