home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11545 < prev    next >
Encoding:
Text File  |  1992-07-23  |  3.4 KB  |  101 lines

  1. Path: sparky!uunet!gatech!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
  2. From: dave@cs.arizona.edu (Dave Schaumann)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HIgher problems at hand. HELP!
  5. Message-ID: <1992Jul24.010254.25873@organpipe.uug.arizona.edu>
  6. Date: 24 Jul 92 01:02:54 GMT
  7. References: <Brv6oD.Dq5@usenet.ucs.indiana.edu>
  8. Sender: news@organpipe.uug.arizona.edu
  9. Reply-To: dave@cs.arizona.edu (Dave Schaumann)
  10. Organization: University of Arizona
  11. Lines: 87
  12. In-Reply-To: shulick@navajo.ucs.indiana.edu (Sam Hulick)
  13.  
  14. In article <Brv6oD.Dq5@usenet.ucs.indiana.edu>, shulick@navajo (Sam Hulick) writes:
  15. >What's wrong with this subroutine?
  16.  
  17. Since you asked...
  18.  
  19. >int strstr(char *data, char *chunk) 
  20.                   ^^^^        ^^^^^
  21. I'm not thrilled with your variable names.  "text" and "pattern", I think,
  22. would've been better choices.
  23.  
  24. >        static int x;                                        
  25.          ^^^^^^
  26. Why make x static?  It serves no purpose.  (Unless statically allocated
  27. variables run significantly faster on your compiler.  Certainly the 68000
  28. has enough registers that this variable could easily be put in one)
  29.  
  30. >        for (x=0;x<strlen(data); ++x)   {                    
  31.                   ^^^^^^^^^^^^^^
  32. A little white space goes a long way toward readability.  Also, strlen()
  33. is needlessly wasteful here.  This is better writtin, I think, as
  34.  
  35.          for( x = 0 ; data[x] != '\0' ; x++ ) {
  36.  
  37. Better yet, you could eliminate x all together:
  38.  
  39.     for( ; *data ; data++ )
  40.  
  41. (of course, making the necessary changes in the loop body)
  42.  
  43. >                if (!strncmp(chunk, &data[x], strlen(chunk)))
  44.                      ^^^^^^^^                  ^^^^^^
  45. I'm not too fond of using a boolean operator on strncmp(), since it returns
  46. 3 value ranges, and not two.  And again, you have another call to strlen
  47. inside your loop.  While this one appears to be necessary, you should've
  48. called this once outside the loop, and then stored the value in a variable.
  49.  
  50. >                        return x+1;                          
  51.                                 ^^^
  52. x+1?  At this point, x is the index of the matched pattern in the text.  You
  53. should return x here.
  54.  
  55. Also, according to my manpage, 
  56.  
  57.      char *strstr( char *s1, char *s2 )
  58.  
  59.      strstr() returns a pointer to the first  occurrence  of  the
  60.      pattern  string  s2  in  s1.   For example, if s1 is "string
  61.      thing" and s2 is "ing", strstr() returns "ing thing".  If s2
  62.      does not occur in s1, strstr() returns NULL.
  63.  
  64. From your statement
  65. > d  = strstr("foobar", "bar"); and d comes up like 139579275
  66.  
  67. I would say that your library /does/ have strstr(), but it's not doing
  68. what you expect.  Try
  69.  
  70.     printf( "%s\n", strstr( "foobar", "bar" ) ;
  71.  
  72. and see what happens.
  73.  
  74. If you really want a routine that does what you described, try this:
  75.  
  76. int find( char *text, char *pattern ) {
  77.  
  78.   /* Depending on how good your compiler is at optimization, you may */
  79.   /* want to use the `register' modifier on t's declaration. */
  80.  
  81.   char *t ;
  82.   int plen = strlen(pattern) ;
  83.   for( t = text ; *t ; t++ )
  84.     if( strncmp(t, pattern, plen) == 0 )
  85.       return t - text ;
  86.   return -1 ;
  87.   }
  88.  
  89. This still has its problems (most notably, it will continue to loop even when
  90. pattern is longer than t), but it should be significantly faster than yours,
  91. and IMHO it's a lot clearer too.
  92.  
  93. -- 
  94. Mary had a swingin' lamb/he followed her to
  95.     school
  96.  
  97. She hocked his wool for a bongo drum and man that lamb was
  98.     cool.
  99.  
  100.         -Mr. Know-it-all
  101.