scanning string for ![0..9]

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

    #1

    scanning string for ![0..9]

    Is there a better way to check if a char* contains
    characters that are not decimal numbers?

    TIA

    // quickly scan str, return if something is outside [0..9]
    count = 0;
    while (str[count] != '\0') {
    if ((str[count] < '0') || (str[count] > '9')) {
    return INVALID_CHAR;
    }
    count++;
    }


  • pete

    #2
    Re: scanning string for ![0..9]

    aurgathor wrote:[color=blue]
    >
    > Is there a better way to check if a char* contains
    > characters that are not decimal numbers?[/color]

    There's another way.
    I don't know if it's better.
    [color=blue]
    > // quickly scan str, return if something is outside [0..9]
    > count = 0;
    > while (str[count] != '\0') {
    > if ((str[count] < '0') || (str[count] > '9')) {
    > return INVALID_CHAR;
    > }
    > count++;
    > }[/color]

    #include <ctype.h>

    for (count = 0; str[count] != '\0'; ++count) {
    if (!isdigit((unsi gned char)str[count])) {
    return INVALID_CHAR;
    }
    }

    --
    pete

    Comment

    • Alex Fraser

      #3
      Re: scanning string for ![0..9]

      "aurgathor" <spam-me-if@you.com> wrote in message
      news:1111230253 .50c5ef0f6fe03a da1e7834f618195 e3a@teranews...[color=blue]
      > Is there a better way to check if a char* contains
      > characters that are not decimal numbers?
      >
      > TIA
      >
      > // quickly scan str, return if something is outside [0..9]
      > count = 0;
      > while (str[count] != '\0') {
      > if ((str[count] < '0') || (str[count] > '9')) {
      > return INVALID_CHAR;
      > }
      > count++;
      > }[/color]

      Alternative, debatable if it's better:

      #include <ctype.h>

      char *p = str;
      while (*p)
      if (!isdigit((unsi gned char)*p++)) return INVALID_CHAR;

      Alex


      Comment

      • CBFalconer

        #4
        Re: scanning string for ![0..9]

        aurgathor wrote:[color=blue]
        >
        > Is there a better way to check if a char* contains
        > characters that are not decimal numbers?
        >
        > // quickly scan str, return if something is outside [0..9]
        > count = 0;
        > while (str[count] != '\0') {
        > if ((str[count] < '0') || (str[count] > '9')) {
        > return INVALID_CHAR;
        > }
        > count++;
        > }[/color]

        Don't use // comments in newsgroups. They don't survive line
        wrapping very well, and are illegal in the most prevalent standard,
        C90.

        #include <ctype.h>
        ...
        int digitsonly(char *str) {
        while (*str)
        if (!(isdigit(unsi gned char)*str++)) return INVALID_CHAR;
        return ALL_DIGS;
        }

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson

        Comment

        • Tor Rustad

          #5
          Re: scanning string for ![0..9]

          "aurgathor" <spam-me-if@you.com> wrote in message
          [color=blue]
          > Is there a better way to check if a char* contains
          > characters that are not decimal numbers?[/color]


          strcspn(str, "0123456789 ")


          --
          Tor <torust AT online DOT no>

          Comment

          • Andrey Tarasevich

            #6
            Re: scanning string for ![0..9]

            aurgathor wrote:
            [color=blue]
            > Is there a better way to check if a char* contains
            > characters that are not decimal numbers?
            >
            > TIA
            >
            > // quickly scan str, return if something is outside [0..9]
            > count = 0;
            > while (str[count] != '\0') {
            > if ((str[count] < '0') || (str[count] > '9')) {
            > return INVALID_CHAR;
            > }
            > count++;
            > }[/color]

            It depends on what you mean by "better". Shorter? Faster?


            int count = 0;
            sscanf(str, "%*[0123456789]%n", &count);
            if (str[count] != '\0)
            return INVALID_CHAR;

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Stephen Sprunk

              #7
              Re: scanning string for ![0..9]

              "aurgathor" <spam-me-if@you.com> wrote in message
              news:1111230253 .50c5ef0f6fe03a da1e7834f618195 e3a@teranews...[color=blue]
              > Is there a better way to check if a char* contains
              > characters that are not decimal numbers?
              >
              > TIA
              >
              > // quickly scan str, return if something is outside [0..9]
              > count = 0;
              > while (str[count] != '\0') {
              > if ((str[count] < '0') || (str[count] > '9')) {
              > return INVALID_CHAR;
              > }
              > count++;
              > }[/color]

              Take a look at isdigit(). Be sure to #include <ctype.h>.

              S

              --
              Stephen Sprunk "Stupid people surround themselves with smart
              CCIE #3723 people. Smart people surround themselves with
              K5SSS smart people who disagree with them." --Aaron Sorkin

              Comment

              • Chris Torek

                #8
                Re: scanning string for ![0..9]

                >"aurgathor" <spam-me-if@you.com> wrote in message[color=blue][color=green]
                >> Is there a better way to check if a char* contains
                >> characters that are not decimal numbers?[/color][/color]

                In article <AQX_d.2739$ai7 .66229@news2.e. nsc.no>
                Tor Rustad <torust@online. no.spam> wrote:[color=blue]
                >strcspn(str, "0123456789 ")[/color]

                This will only count leading non-digits. For instance, the result
                for "abc123def" will be 3, but for "123abcdef" it will be 0.

                A somewhat twisted method is to use strtol or strtoul in combination
                with strspn or strcspn to check for leading whitespace and/or sign:

                int has_nondigit;
                char *ep;

                has_nondigit = strcspn(str, "0123456789 ") != 0 ||
                (strtoul(str, &ep, 10), *ep != '\0');
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                • Eric Sosman

                  #9
                  Re: scanning string for ![0..9]

                  Chris Torek wrote:[color=blue][color=green]
                  >>"aurgathor" <spam-me-if@you.com> wrote in message
                  >>[color=darkred]
                  >>>Is there a better way to check if a char* contains
                  >>>characters that are not decimal numbers?[/color][/color]
                  >
                  >
                  > In article <AQX_d.2739$ai7 .66229@news2.e. nsc.no>
                  > Tor Rustad <torust@online. no.spam> wrote:
                  >[color=green]
                  >>strcspn(str , "0123456789 ")[/color]
                  >
                  >
                  > This will only count leading non-digits. For instance, the result
                  > for "abc123def" will be 3, but for "123abcdef" it will be 0.
                  >
                  > A somewhat twisted method is to use strtol or strtoul in combination
                  > with strspn or strcspn to check for leading whitespace and/or sign:
                  >
                  > int has_nondigit;
                  > char *ep;
                  >
                  > has_nondigit = strcspn(str, "0123456789 ") != 0 ||
                  > (strtoul(str, &ep, 10), *ep != '\0');[/color]

                  A possibly simpler approach that more closely matches
                  the problem statement might be

                  has_nondigit = str[ strspn(str, "0123456789 ") ] != '\0';

                  At about this point, though, I think I'd take a step
                  back and ask why the O.P. wants to determine whether a
                  string consists only of digits. If the intent is to convert
                  to a number, it's probably simpler to go ahead and convert
                  with strtol() or strtoul() and check that it succeeded and
                  converted the entire string; the conversion will detect and
                  report any non-digit it encounters.

                  Sometimes (not always, but sometimes) solving the stated
                  problem is less useful than coming up with a better statement.

                  --
                  Eric Sosman
                  esosman@acm-dot-org.invalid

                  Comment

                  • Tor Rustad

                    #10
                    Re: scanning string for ![0..9]

                    "Chris Torek" <nospam@torek.n et> wrote in message[color=blue][color=green]
                    > >"aurgathor" <spam-me-if@you.com> wrote in message[color=darkred]
                    > >> Is there a better way to check if a char* contains
                    > >> characters that are not decimal numbers?[/color][/color]
                    >
                    > In article <AQX_d.2739$ai7 .66229@news2.e. nsc.no>
                    > Tor Rustad <torust@online. no.spam> wrote:[color=green]
                    > >strcspn(str, "0123456789 ")[/color]
                    >
                    > This will only count leading non-digits. For instance, the result
                    > for "abc123def" will be 3, but for "123abcdef" it will be 0.[/color]

                    True, if the lenght of digits is wanted

                    strspn(str, "0123456789 ")

                    --
                    Tor <torust AT online DOT no>

                    Comment

                    Working...