Strings

This is a discussion on Strings within the C Programming forums, part of the General Programming Boards category; Hey all, I can't figure out how to do this at all.... hopeing someone could help. I have a string ...

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    Strings

    Hey all,

    I can't figure out how to do this at all.... hopeing someone could help.

    I have a string containing groups of two charactors. For the sake of this article they can be random:

    "bh oj wh qx"

    The string varies in length, ie in number of groups, but always has at least 2 and no more than 6 groups.

    I need to search this string to see if ANY group of charactors occues twice. For instance:

    "aj df qw xa df"

    So in pseudocode, I figure I can do something like this:

    Code:
    For each group in string
             check with all other groups in that string to see if its equal
    I've been unable to find any functions on google that tell me if a string contains a groups of chars like this.

    An help woud be highly apreiciated.
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up strstr.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    From http://www.phim.unibe.ch/comp_doc/c_...PLES/strstr.c:

    Code:
    char string[]="string to search";
     char   test[]="sear";
    			
    		/* strstr returns a pointer into 'string'
    		 * if 'test' is found' if not found, NULL 
    		 * is returned.				*/
     
     if (strstr(string, test)) puts("String found");
    So... that leaves the question, how can I do a for loop for each of the groups, rather than a single char? I'm sorry... strings and poitners are absolutly my weak point.

    Thanks
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You can loop it, and when it find the occurance of the substring it will return a pointer to it and then you can use that as the source string for another round of strstr()'s.

  5. #5
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    OK, but I'm not searching for a particular starting string, I'm searching for only second occurences of groups already present. Running through every 2-char combo would be a little inefficient...

    Thanks
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something along this line?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       const char example[] = "aj df qww xa df ao aj df qww";
       const char first[7], *start = example, *match, *ptr;
       while ( sscanf(start, "%6s", first) == 1 )
       {
          size_t len = strlen(first);
          printf("first = \"%s\"\n", first);
          if ( !start[len] )
          {
             break;
          }
          start += len + 1;
          ptr = start;
          while ( match = strstr(ptr, first) )
          {
             printf(" match = \"%s\"\n", match);
             ptr = match + 3;
          }
       }
       return 0;
    }
    
    /* my output
    first = "aj"
     match = "aj df qww"
    first = "df"
     match = "df ao aj df qww"
     match = "df qww"
    first = "qww"
     match = "qww"
    first = "xa"
    first = "df"
     match = "df qww"
    first = "ao"
    first = "aj"
    first = "df"
    first = "qww"
    */
    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.*

  7. #7
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Yeah, the algorithm may be as this:

    1. extract substring.
    2. traverse the storage, looking for duplicate (storage explained below)
    2. 'remember' substring in some storage (put it in the array, linked list, hash etc)
    3. loop to step 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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