string search question...

This is a discussion on string search question... within the C Programming forums, part of the General Programming Boards category; What is the preferred method to search for a string and then copy only part of the string? For instance. ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question string search question...

    What is the preferred method to search for a string and then copy only part of the string? For instance. I want to search for a string called:
    "@foobar@"
    I only want to copy the "foobar" part.

    Thanks!
    Flight

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> preferred method
    The preferred method is one that you are comfortable using, and does it's job correctly and efficiently.

    You can use strstr() to search the string.

    There is no need to actually copy from the array you're searching, as you already know what that text is.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char name[] = "@hammer@";
      char foo[BUFSIZ];
    	
      if (strstr(name, "hammer"))
      {
        strcpy (foo, "hammer");
        puts(foo);
      }
    	
      return(0);
    }
    There are other ways...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    If all you want is a string between @'s you can do something along the lines of this:
    Code:
    Tue Sep 23 7:18:37pm
    sangut: ~/misc
    > cat rp.c
    #include <stdio.h>
    #include <string.h>
    
    char *copy(char *string, char *buffer) {
      size_t a;
      size_t b;
    
      a = strspn(string, "@");
      b = strcspn(string + a, "@");
      strncpy(buffer, string + a, b);
      buffer[b] = '\0';
    
      return buffer;
    }
    
    int main ( ) {
      char buffer[7];
    
      copy("@foobar@", buffer);
      puts(buffer);
    
      return 0;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    I believe you can use the strstr() function...

    char *strstr(const char *string, const char *substring)

    char *myString = "@foobar@";
    char *target = "foobar";
    char *result


    result = strstr(myString, target) -- try that.

    if the substring is not found, it will return NULL.

    John

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If you want to copy everything between the @ and the @, here is another idea.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char text[] = "@foobar@";
       char found[ sizeof text ];
       if ( sscanf(text, "@%[^@]@", found) == 1 )
       {
          puts(found);
       }
       return 0;
    }
    
    
    /* my output
    foobar
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you want to copy everything between the @ and the @, here is another idea.
    Posted more for fun than anything else...
    Code:
    #include <stdio.h>
    
    char *foo(const char *input, char delim, char *output, size_t outsize)
    {
      /* Returns NULL if 2 delimiters aren't found or we run out of room in the output buffer */
      char *p = output;
      while (*input && *input != delim) input++;
      while (*input && *++input != delim && --outsize) *output++ = *input;
      *output = '\0';
      if (*input != delim)
        p = NULL;
      return p;
    }
    
    int main(void)
    {
      char name[] = "@hammer@";
      char bar[BUFSIZ];
    
      printf ("%s -->  %s\n", name, foo(name, '@', bar, sizeof(bar)));
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink

    funny...and thx hammer

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    I guess I was tired when I originally wrote this. What I really need to do is be able to do the following:

    1. Search for multiple occurances of a string.
    2. Each string that I ultimately want to write to an output file will be on a line that immediately follows a line with the string "@some_name@".

    for example: //@some_name@
    // process_data
    where "process_data" is the string I need.

    3. I would then need to be able to search for other strings that would have different names and write those to a file as well.

    Thanks!
    Flight

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You aren't posting much code. I, and possibly others, may be less inclined to offer assistance if you don't show much of an effort.

    However, the following should demonstrate some sort of starting point.
    Code:
    /*
     * @DESCRIPTION@
     * Print the line following a line containing at least one keyword.
     */
    #include <stdio.h>
    #include <string.h>
    
    /* @FUNCTION@ */
    void foo(const char *line)
    {
       fputs(line, stdout); /* or do further processing */
    }
    
    /* @FUNCTION@ */
    void bar(const char *filename, const char *keyword[], size_t size)
    {
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          int printnext = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             size_t i;
             if ( printnext )
             {
                foo(line); /* keyword found in previous line */
                printnext = 0;
             }
             for ( i = 0; i < size; ++i )
             {
                if ( strstr(line, keyword[i]) != NULL )
                {
                   printnext = 1; /* found keyword in current line */
                   break;
                }
             }
          }
       }
    }
    
    /* @FUNCTION@ */
    int main(void)
    {
       const char *keyword[] = { "@FUNCTION@", "@DESCRIPTION@" };
       bar(__FILE__, keyword, sizeof(keyword)/sizeof(*keyword));
       return 0;
    }
    
    /* my output
     * Print the line following a line containing at least one keyword.
    void foo(const char *line)
    void bar(const char *filename, const char *keyword[], size_t size)
    int main(void)
       bar(__FILE__, keyword, sizeof(keyword)/sizeof(*keyword));
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink

    Dave, you're right I apologize. I did previously post code although I have changed it quite a bit. The only question I really need answered is:
    When I locate the string I want to copy, how do I copy it and into what?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>When I locate the string I want to copy, >>how do I copy it
    Methods as above, including strcpy() etc.

    >>and into what?
    Whatever you like, but it'll have to ultimately be another char array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Any Quick help Please!

    I have tried to develop the following algorithm for my proble, but i need help putting it into codes. I was told by a prof that i can achieve it with 8-10 lines of codes.
    Then i tried, but i don't think my code's right, cuz it didn't work.


    - Use char buf[BUFSIZ]
    -Proceed to read each line from stdin, using fgets in a while loop
    -Look through each character in the line for the string i want
    -Using strncmp, check if you find a match for the old string
    -if the "old" string is found
    - print out the "new" string in its place
    -increase buf by number of characters in the "new" string
    -else if you can't find a match, just print the characters.


    Now here's how i put it into code and it won't work.
    Help please!!

    Thanks a lot people.
    I had posted my problem before, it's just not working yet. I need to replace an old string with a new one from a line as read in from stdin, onto the stdout.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM
  5. Replies: 2
    Last Post: 05-05-2002, 02:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21