Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

check string in c

Name: Anonymous 2009-05-24 9:14

what the fast way to check that string contain only characters within some set like "a-z0-9"?

Name: Anonymous 2009-05-24 9:15

multiple threads XD

Name: Anonymous 2009-05-24 9:21


int i;

if(sscanf(s, "%*[a-z0-9]%n", &i) > 0 && s[i] == 0)

Name: Anonymous 2009-05-24 9:26

>>3
Thanks i'll try that.

Name: Anonymous 2009-05-24 9:29

>>3
[b][u]DONT HELP HIM[/i][/b]

Name: Anonymous 2009-05-24 9:59

Use Regular Expressions.

Name: Anonymous 2009-05-24 10:24

>>6
[b][u]YOU JUST ANGRED AN FUQEN REGUlAR EXPRESSION PROGRAMMER[/][/b]

Name: Anonymous 2009-05-24 10:43

>>7
Is that covered in Computer Science III?

Name: Anonymous 2009-05-24 10:57

>>8
Return to Encyclopedia Dramatica, please.

Name: Anonymous 2009-05-24 11:29

>>9
Fuck you, I was there when it happened.

Name: Anonymous 2009-05-24 11:34

>>5
>>7
Are you even trying?

Name: Anonymous 2009-05-24 11:43

>>10
Return to /b/, please.

Name: Anonymous 2009-05-24 11:47

Double, double, toil and trouble
Fire burn and hax my anus bubble.

Name: Anonymous 2009-05-24 12:01

>>3,1
if (!s1[strspn(s1, "abcdefghijklmnopqrstuvwxyz0123456789")])

Name: Anonymous 2009-05-24 12:14

use sed

Name: Anonymous 2009-05-24 13:17

>>14
Seems even faster.

Name: Anonymous 2009-05-24 13:19

>>14 strspn
EXPERT C standard library user.

Name: Anonymous 2009-05-24 13:49

Embed lua.

Name: Anonymous 2009-05-24 13:53

>>14
This approach stinks, faggot. You are allocating 36 bytes in your .rodata section. What a poor way of programming!

Name: >>14 2009-05-24 16:27

>>19
I think you mean 37.

Name: Anonymous 2009-05-24 18:01

for(int i=0; i<str_length; i++){
   switch(str[i]){
      case 'a':
          //do something
          break;
      case 'A':
          //do something
          break;
      case 'b':
          //do something
          break;
      case 'B':
          //do something
          break;
      case 'c':
          //do something
          break;
      case 'C':
          //do something
          break;
      case 'd':
          //do something
          break;
      case 'D':
          //do something
          break;
      case 'e':
          //do something
          break;
      case 'E':
          //do something
          break;
      case 'f':
          //do something
          break;
      case 'F':
          //do something
          break;
      case 'g':
          //do something
          break;
      case 'G':
          //do something
          break;
      case 'h':
          //do something
          break;
      case 'H':
          //do something
          break;
      case 'i':
          //do something
          break;
      case 'I':
          //do something
          break;
      case 'j':
          //do something
          break;
      case 'J':
          //do something
          break;
      case 'k':
          //do something
          break;
      case 'K':
          //do something
          break;
      case 'l':
          //do something
          break;
      case 'L':
          //do something
          break;
      case 'm':
          //do something
          break;
      case 'M':
          //do something
          break;
      case 'n':
          //do something
          break;
      case 'N':
          //do something
          break;
      case 'o':
          //do something
          break;
      case 'O':
          //do something
          break;
      case 'p':
          //do something
          break;
      case 'P':
          //do something
          break;
      case 'q':
          //do something
          break;
      case 'Q':
          //do something
          break;
      case 'r':
          //do something
          break;
      case 'R':
          //do something
          break;
      case 's':
          //do something
          break;
      case 'S':
          //do something
          break;
      case 't':
          //do something
          break;
      case 'T':
      .........
   }
}

Name: Anonymous 2009-05-24 18:09

>>19
oh noes 36 bytes, whatever will we do says the person who spent a week shrinking his Uni project to a quarter it's original size

Name: Anonymous 2009-05-24 19:49

>>22
This one time I developed a game in Sepples for a Uni project. I ended up running out of time so I tried to focus on finishing the actual game. Even though I did complete it, I ended up turning it in with 29 memory leaks still there and I feel kind of bad about it :(

Name: Anonymous 2009-05-24 19:50

i implemented mozart's rondo alla turca in lisp

Name: Anonymous 2009-05-24 22:05

>>23
What grade did you end up with?

Name: Anonymous 2009-05-24 22:10

>>25
A lowly B+

Name: Anonymous 2009-05-24 22:17

C's string handling sucks. Length-prefix is obviously superior.

Trying to handle strings fast and efficiently in C is like trying to build web servers in lisp.

Name: Anonymous 2009-05-24 22:21

>>27
Length-prefix is obviously superior.
Ironic, thats exactly how C string handling works in my string struct. I think it's you who doesn't understand the basic idea of C in that there is nothing you can't do yourself.

Name: Anonymous 2009-05-25 1:15

>>28
So what you're saying is that it has nearly zero value to the modern programmer. If it didn't allow such tight control over memory, it would definitely be worthless for any practical purposes.

Name: Anonymous 2009-05-25 4:15

>>27
What's wrong with building web servers in Lisp? Edi's even done all the work for you.

Name: Anonymous 2009-05-25 5:11

Google is written in LISP

Name: Anonymous 2009-05-25 8:33

Bump for all to have a laugh at >>27 who doesn't understand C's strings are primitive and this "length prefix" is a feature on top of them.

Name: Anonymous 2009-05-25 8:45

>>32
Play nice, it's not his fault he's retarded

Name: Anonymous 2009-05-25 10:46

int is_inset(const char *s){
    if(*s=='\0')
        return 1;
    if(*s>='a' && *s<='z' || *s>='0' && *s<='9')
        return is_inset(s+1);
   
    return 0;
}

Name: Anonymous 2009-05-25 11:06

>>34
Superior iterative version.

int is_inset(const char *s)
{
    while (*s)
    {
        if (!(*s>='a' && *s<='z' || *s>='0' && *s<='9'))
            return 0;
        ++s;
    }
    return 1;
}

Name: Anonymous 2009-05-25 12:01

>>35
How is it superior if it uses fucking failure(TM) indentation style?

Name: Anonymous 2009-05-25 12:31

>>36
This is C, not Haskell. We don't support purely fictional indentation styles here.

Name: Anonymous 2009-05-25 12:33

>>36
fucking failure? You don't know what you're talking about. Indentation in >>34 is PIG DISGUSTING!!

Name: Anonymous 2009-05-25 12:33

>>36
Faggot. I bet you enjoy single closing braces with no sign of its opening brother in the vertical line, faggot.

Name: Anonymous 2009-05-25 12:35

>>35-39
Idiots, GNU-style indentation is superior

Name: Anonymous 2009-05-25 13:20

>>40
Excuse me, could you repeat that? It was kind of hard to make out with all that cocks stuffed in your mouth.

Name: Anonymous 2009-05-25 13:26

"All that cocks stuffed"

English major.

Name: Anonymous 2009-05-25 13:26

"It was kind of hard"
-- >>41

Name: Anonymous 2009-05-25 13:29

Whenever I write in C, I use the >>35 style of indentation.

Name: Anonymous 2009-05-25 13:30

Anyone who doesn't use allman-based style is a flaming cretin. But I guess some people prefer to line up their closing braces not to their respective opening braces, but to something like the fuqin int fun.

Name: Anonymous 2009-05-25 13:35

>>44
Aww, I already had a post that involved a chest of cocks and a dicksage, but then I clicked the link and realised that you use the correct indention. :(

Name: Anonymous 2009-05-25 13:41

int
is_inset(s)
const char *s;
  {
    while (*s)
      {
        if (!(*s>='a' && *s<='z' || *s>='0' && *s<='9'))
          {
            return 0;
          }
        ++s;
      }
    return 1;
}


Fixed you're code.

Name: Anonymous 2009-05-25 13:43

>>35 is the indentation style that my highschool C teacher taught to the class, and they always asked me help fix their code whenever the screwed it up. Because of that I have always and will forever associate that style with complete idiots.

Name: Anonymous 2009-05-25 13:48

>>47
WHAT
THE
FUCK
 BRO

Code is supposed to look tidy.

Name: Anonymous 2009-05-25 13:56

>>49
I messed up that last brace, but with that fixed it would look beautiful.

Name: Anonymous 2009-05-25 13:58

>>48
The braces on their own lines make simple functions too long but every other style looks weird to me. I'd rather FOIC than C, but sometimes C is best.

Name: Anonymous 2009-05-25 14:12

>>51
For very short functions (like inline ones) I use
int max(int a, int b) {
    return (a > b ? a : b); }

Nearly like FIOC.

Name: Anonymous 2009-05-25 14:17

int
main
                      (
 int argc, char **argv
)
          {
 return 0;
}

Name: Anonymous 2009-05-25 14:25

>>53
This is now the official indentation style of /prog/

Name: Anonymous 2009-05-25 14:31

>>54
I should'f patented it.

Name: Anonymous 2009-05-25 14:35

>>53-55
Clarification required: is the opening brace placed after the end of the line following it, or after the longest line in the block following it?

Name: Anonymous 2009-05-25 14:40

>>52
You know you can skip the braces if you only have one line, right?

Name: Anonymous 2009-05-25 14:43

>>57
I know you can do that when you have if statements etc., but is it possible with functions?

Name: Anonymous 2009-05-25 14:44

>>56
The latter.
int
main
              (
 int     argc,
 char  **argv
)
          {
 return 0;
}

Name: Anonymous 2009-05-25 14:46

>>59
Though that example doesn't really show it.
int
main
              (
 int     argc,
 char  **argv
)
                    {
 const char *s;
 s = "Hello world!";
 printf("%s", s);
 return 0;
}

Name: Anonymous 2009-05-25 15:13

int is_inset(const char*s){while(*s){if (!((*s>='a'&&*s<='z')||(*s>='0'&&*s<='9')))return 0;++s;}return 1;}
BEST STYLE

Name: Anonymous 2009-05-25 15:21

>>60
Excellent. I shall adopt this style.

Name: Anonymous 2009-05-25 15:22

>>61
Amateur optimizer. You forgot one space after if.

Name: Anonymous 2009-05-25 15:24

>>62
Me to.

We could use it as a Shibboleth.

Name: Anonymous 2009-05-25 15:32

>>60
Nested blocks:

int main
                      (
 int argc, char **argv
)
                         {
 int i = 1;
 while
          (
  i < argv
 )
                        {
  printf("%s", argc[i]);
  ++i;
 }
 return 0;
}


I wish to hear your suggestions!!

Name: Anonymous 2009-05-25 15:42

>>65
int main
                      (
 int argc, char **argv
)
                            {
 int a, b, c, d, e, f;
 if (b)
            {
  a++++++--;
 }
 return (d+e+f > a)?'y':'n';
}


Works for me.

Name: Anonymous 2009-05-25 15:42

>>65
line breaks in the for loops.

Name: Anonymous 2009-05-25 15:43

>>63
Oh snap!
Allow me to re-optimize:
int is_inset(const char*s){while(*s){if(!((*s>='a'&&*s<='z')||(*s>='0'&&*s<='9')))return 0;++s;}return 1;}

Name: Anonymous 2009-05-25 15:45

>>67
As in
for
           (
 int i = 0;
 i < argc;
 ++i
)
               {
 //do something
}

?

Name: Anonymous 2009-05-25 15:49

>>68
int is_inset
                   (
const char*s
)
                   {
while
                   (
*s
)
                   {
if
                   (
!
                   (
                   (
*s>='a'
&&
*s<='z'
)
||
                   (
*s>='0'
&&
*s<='9'
)
)
)
return 0;
++s;
}
return 1;
}

Name: Anonymous 2009-05-25 15:55

>>68
is_inset(char*s){while(*s){if(!(*s>'`'&&*s<'{'||*s>'/'&&*s<':'))return 0;++s;}return 1;}

Name: Anonymous 2009-05-25 15:57

>>68,70

int
              is_inset
                 (
    const             char
  *           s              )
       {                while
(   *s    
                   )  {
 if       (    !(               ( *s
    >=        'a'   
                      &&
                           *s
    <=           'z'               )
 ||          (          *s
        >=
               '0'
                       &&
  *s                           <=
          '9'       )))
      return
                        0     ;
    ++s          ;         }
           return        1
                   ;
}

Name: Anonymous 2009-05-25 16:01

>>71
A little faster using Fast Logic operators.
is_inset(char*s){while(*s){if((*s<'0'|*s>'9')&(*s<'a'|*s<'z'))return 0;++s;}return 1;}

Name: Anonymous 2009-05-25 16:02

>>73
(*s<'a'|*s<'z')
I think you missed something.

Name: Anonymous 2009-05-25 16:02

>>72
This is the most amazing indentation I've seen in my life. Please take me as your student, master.

I think I can see a face in there

Name: Anonymous 2009-05-25 16:05

>>74
Thanks Anonymous.

Here's the next iteration. I'm not sure about the return value though, it might need one more !.

is_inset(char*s){while(*s&(*s<'0'|*s>'9')&(*s<'a'|*s>'z'));return!*s;}

Next up is removing the extra set of parentheses.

Name: Anonymous 2009-05-25 16:07

>>76
There, much better.

is_inset(char*s){while(*s&&*s<'0'|*s>'9'&&*s<'a'|*s<'z');return!*s;}

Name: Anonymous 2009-05-25 16:08

>>77
Forgot to change the < to a > here.

is_inset(char*s){while(*s&&*s<'0'|*s>'9'&&*s<'a'|*s>'z');return!*s;}

Name: Anonymous 2009-05-25 16:10

>>75
Can you answer this question: TARO or Xarn?

Name: Anonymous 2009-05-25 16:11

>>78
Argh, forgot && only evaluates as much as is needed. Also needs an increment of s.

Name: Anonymous 2009-05-25 16:32

>>80
All right, this code should actually *work*:

is_inset(char*s){while(*s&&*s>'/'&*s<':'|*s>'`'&*s<'{')++s;return!*s;}

Name: Anonymous 2009-05-25 16:35

is_inset(char*s){while(*s&&*s>47&*s<58|*s>96&*s<123)++s;return!*s;}

Name: Anonymous 2009-05-25 16:37

is_inset(char*s){for(;*s&&*s>47&*s<58|*s>96&*s<123;++s);return!*s;}

Name: Anonymous 2009-05-25 16:37

>>81
Nice. But are you sure that &*s won't just return s?

Name: Anonymous 2009-05-25 16:47

>>84
Yes, there would be a parsing error otherwise (a value followed by another value without an operator in between).

Name: Anonymous 2009-05-25 16:52

Finally managed to move that incement to the condition.

is_inset(char*s){for(;*s>47&*s<58|*s>96&*s<123&&*s++;);return!*s;}

Name: Anonymous 2009-05-25 17:06

*s<123&&*s++
Remember that && short-circuits.

Name: Anonymous 2009-05-25 17:07

>>87
Goddamn that &&, always foiling my plans!!

Name: Anonymous 2009-05-25 22:30

>>88
Who said C optimization is easy?

Name: Anonymous 2009-05-26 0:05

>>86,88
comma operator.

Name: Anonymous 2009-05-26 3:39

>>90
Yeah, maybe I don't need the condition since the zero check would fall outside the ranges anyway.

is_inset(char*s){for(;*s>47&*s<58|*s>96&*s<123;++s);return!*s;}

Name: Anonymous 2009-05-26 4:12

sub is_inset{$_[0]!~/[^\da-z]/}

Name: ​​​​​​​​​​ 2010-10-23 3:29

Name: Anonymous 2010-12-09 14:41


Don't change these.
Name: Email:
Entire Thread Thread List