Thread: str_replace && str_find

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    str_replace && str_find

    Hi!
    I need two functions. One that replaces text in a string and other that finds a string and returns its location. I know that there are strstr function, but it returns pointer to that string, not it's location. I have written my version of both of these functions, but they work too slow! Thank you!

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty certain no-one here want's to do your homework....
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    And if you've already written them yourself, post them up (or links to them if they're too big) so we can try and help optimise them.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Its not my homework!!!

    Code:
    int strfnd(char *buffer, char *string, unsigned int position){
    
    	unsigned int i, ret = 0;
    	
    	for(i=position; i<strlen(buffer); i++){
    		if(strncmp(&buffer[i], string, strlen(string)) == 0){
    			ret = i;
    			break;
    		}
    	}
    	
    	return ret; // return string position
    	
    }
    Well, it works too slow. Aren't there really any functions?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look how many times you're calling strlen()
    Call it once for each string and save the result in a local
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    It seems to me that this:

    Code:
    for(i=position; i<strlen(buffer); i++){
    ...may slow things down on you a bit anyway. You are calling strlen() every time through the loop. Why not do one call to strlen() before the loop, and assign it to a variable. Then you can use that variable in the loop for your limit.

    edit::

    gah! Salem you beat me! On the positive side, I guess saying the same thing as you is not bad.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I know that there are strstr function, but it returns pointer to that string, not it's location.
    To determine the position, do this:
    Code:
    int pos = (strstr(instring, searchstring)-instring);
    (That assumes that the string contains what your looking for.)
    Code:
    char *p;
    if(p=strstr(instring, searchstring)) {
        int pos = p-instring;
        printf("String position: %d\n", pos);
    }
    else {
        /* string not found */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Thank you guys very much! And what about replace function? Does this function exists?

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by q6z4k
    Thank you guys very much! And what about replace function? Does this function exists?
    Show us your code.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A comparison
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int strfnd1(char *buffer, char *string, unsigned int position)
    {
      unsigned int i, ret = 0;
    
      for (i = position; i < strlen(buffer); i++) {
        if (strncmp(&buffer[i], string, strlen(string)) == 0) {
          ret = i;
          break;
        }
      }
      return ret;
    }
    
    /* pre-calculate string lengths */
    int strfnd2(char *buffer, char *string, unsigned int position)
    {
      unsigned int i, ret = 0;
      unsigned int blen = strlen(buffer);
      unsigned int slen = strlen(string);
    
      for (i = position; i < blen; i++) {
        if (strncmp(&buffer[i], string, slen) == 0) {
          ret = i;
          break;
        }
      }
      return ret;
    }
    
    /* pre-calculate string lengths */
    /* only call strncmp if first chars are actually equal (saving function calls) */
    int strfnd3(char *buffer, char *string, unsigned int position)
    {
      unsigned int i, ret = 0;
      unsigned int blen = strlen(buffer);
      unsigned int slen = strlen(string);
    
      for (i = position; i < blen; i++) {
        if ( buffer[i] == string[0] &&
             strncmp(&buffer[i], string, slen) == 0) {
          ret = i;
          break;
        }
      }
      return ret;
    }
    
    /* using pointer arithmetic */
    int strfnd4(char *buffer, char *string, unsigned int position)
    {
      unsigned int ret = 0;
      unsigned int blen = strlen(buffer);
      unsigned int slen = strlen(string);
      char *bstart, *bend = buffer + blen;
    
      for ( bstart = buffer + position; bstart < bend; bstart++ ) {
        if ( *bstart == *string &&
             strncmp(bstart, string, slen) == 0) {
          ret = bstart - buffer;
          break;
        }
      }
      return ret;
    }
    
    /* read the processor fast clock */
    #define RDTSC(llptr) { \
        __asm__ __volatile__ ( \
        "rdtsc" \
        : "=A" (llptr) ); \
    }
    
    int main ( void ) {
      char *haystack = "the quick brown fox jumps over the lazy dog";
      char *needle   = "azy";
      struct {
        unsigned long long t1;
        unsigned long long t2;
        int result;
      } r[20];
      int i, n = 0;
    
      /* do each test 100 times */
      RDTSC(r[n].t1);
      for(i=0;i<100;i++) r[n].result = strstr(haystack,needle) - haystack;
      RDTSC(r[n].t2);
      n++;
    
      RDTSC(r[n].t1);
      for(i=0;i<100;i++) r[n].result = strfnd1(haystack,needle,0);
      RDTSC(r[n].t2);
      n++;
    
      RDTSC(r[n].t1);
      for(i=0;i<100;i++) r[n].result = strfnd2(haystack,needle,0);
      RDTSC(r[n].t2);
      n++;
    
      RDTSC(r[n].t1);
      for(i=0;i<100;i++) r[n].result = strfnd3(haystack,needle,0);
      RDTSC(r[n].t2);
      n++;
    
      RDTSC(r[n].t1);
      for(i=0;i<100;i++) r[n].result = strfnd4(haystack,needle,0);
      RDTSC(r[n].t2);
      n++;
    
      for ( i = 0 ; i < n ; i++ ) {
        unsigned long long elapsed = r[i].t2 - r[i].t1;
        printf( "Result=%d, time=%llu\n", r[i].result, elapsed );
      }
    
      return 0;
    }
    
    
    $ gcc -W -Wall -ansi -O2 hello.c
    $ ./a.out
    Result=36, time=21276
    Result=36, time=1708840
    Result=36, time=263308
    Result=36, time=69440
    Result=36, time=61664
    Tough to beat the library strstr() function.

    For those using win32 compilers, the RDTSC needs to be replaced with QueryPerformanceCounter
    Those with non pentium (or pentium compatible) processors need another plan.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    wow! Thank you very much!!!

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    France
    Posts
    9

    The speed of my PC

    Somewhere else I was talking about the speed of my home made / Linux hand compiled kernel PC.

    To give you an idea, I've just cut & paste the code. Here are my results :

    Code:
    shake:~/tmp# ./a.out
    Result=36, time=63
    Result=36, time=102825
    Result=36, time=53586
    Result=36, time=14202
    Result=36, time=14211
    shake:~/tmp#
    The first result is more than 300 times faster than the result of the C example (21276) . I'm amazed.
    Last edited by SurferIX; 05-31-2008 at 02:31 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So I guess it's a good idea to use strstr() . . . .

    By the way, you shouldn't bump threads older than say two weeks. This thread is almost three years old.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ color functions && win98 :P
    By DarkMortar in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 04:45 PM
  2. AnimateWindow && Dev-C++ problems
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 03-13-2006, 04:34 PM
  3. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 08:19 AM
  4. && or ||
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 04:42 PM
  5. hp iPAQ 6300 && C compiler || C# compiler
    By xddxogm3 in forum Tech Board
    Replies: 2
    Last Post: 12-07-2004, 07:28 AM