Help about string & sub-string 
Author Message
 Help about string & sub-string

I need to know how many times a certain sustring is in a bigger string. How
can I implement thi feature with a C function.
TIA

--
Andrea Riciputi



Fri, 14 May 1999 03:00:00 GMT  
 Help about string & sub-string

Quote:

> I need to know how many times a certain sustring is in a bigger string. How
> can I implement thi feature with a C function.
> TIA

You could achieve this with strtok(), look it up in your man pages,
it's probably just the thing you were looking for.

Cheers, flux.



Sat, 15 May 1999 03:00:00 GMT  
 Help about string & sub-string


Quote:

>> I need to know how many times a certain sustring is in a bigger string. How
>> can I implement thi feature with a C function.
>> TIA

>You could achieve this with strtok(), look it up in your man pages,
>it's probably just the thing you were looking for.

How would strtok() help here - it doesn't search for substrings? strstr()
would appear to be more relevant.

--
-----------------------------------------


-----------------------------------------



Sat, 15 May 1999 03:00:00 GMT  
 Help about string & sub-string



: > I need to know how many times a certain sustring is in a bigger string. How
: > can I implement thi feature with a C function.
: > TIA

: You could achieve this with strtok(), look it up in your man pages,
: it's probably just the thing you were looking for.

I don't readily see how strtok() would be useful for this. strstr() in a
loop could do it, like this:

#include <stdio.h>
#include <string.h>

main(int argc, char **argv)
{       char    *sp, *si;
        int     len, count;

        if (argc < 3)
        {       printf("usage: $ %s \"substring\" \"hoststring\" [overlap]\n"
                        , argv[0]);
                exit(0);
        }
        if (argc > 3 && !strcmp("overlap", argv[3]))
                len = 1;
        else
                len = strlen(argv[1]);
        si = argv[2];
        count = 0;
        while (sp = strstr(si, argv[1]))
        {       count++;
                si = sp + len;
        }
        printf("%s\n occurs %d times in\n%s\n", argv[1], count, argv[2]);
        return 0;

Quote:
}

--



Sat, 15 May 1999 03:00:00 GMT  
 Help about string & sub-string

Quote:

>: > I need to know how many times a certain sustring is in a bigger string. How
>: > can I implement thi feature with a C function.

>: You could achieve this with strtok(), look it up in your man pages,

>I don't readily see how strtok() would be useful for this. strstr() in a
>loop could do it, like this:

   ...
>    len = strlen(argv[1]);
>    count = 0;
>    while (sp = strstr(si, argv[1]))
>    {       count++;
>            si = sp + len;
>    }

Your solution works in most cases, but note that it finds two
occurances of abcabc in the string abcabcabcabc when there are
really three.

Both strstr and strtok find occurrences of strings in strings. Which
one to use depends on the problem at hand. strstr works by matching
characters, while strtok identifies tokens that have delimiters
from a specified set of characters. The original poster is
probably looking for the strstr solution, but it isn't absolutely
clear.

For serious string searching you need a modern implementation of
the Boyer-Moore algorithm. Of course, strstr could use the
Boyer-Moore algorithm, but it probably doesn't.



Sun, 16 May 1999 03:00:00 GMT  
 Help about string & sub-string


: >: > I need to know how many times a certain sustring is in a bigger string. How
: >: > can I implement thi feature with a C function.
: >
: >: You could achieve this with strtok(), look it up in your man pages,
: >
: >I don't readily see how strtok() would be useful for this. strstr() in a
: >loop could do it, like this:
: >
:    ...
: >  len = strlen(argv[1]);
: >  count = 0;
: >  while (sp = strstr(si, argv[1]))
: >  {       count++;
: >          si = sp + len;
: >  }

: Your solution works in most cases, but note that it finds two
: occurances of abcabc in the string abcabcabcabc when there are
: really three.

Note that you can add a 3rd argument 'overlap' that will find 3
abcabc's in abcabcabcabc. You have the option of finding overlapping
and non-overlapping substrings. Both bases are covered. You must have
missed that.

: Both strstr and strtok find occurrences of strings in strings. Which
: one to use depends on the problem at hand. strstr works by matching
: characters, while strtok identifies tokens that have delimiters
: from a specified set of characters. The original poster is
: probably looking for the strstr solution, but it isn't absolutely
: clear.

The original article didn't mention identifiable delimiters, that's why
I didn't see an application for strtok() especially if you wanted to
find overlapping substrings or substrings not separated by delimiters.
You might want to reread the thread for clarification.

--



Mon, 17 May 1999 03:00:00 GMT  
 Help about string & sub-string

The only problem with this is that it will not find overlaping strings.
Then again it's up to you if you want to allow them or not.  For example,
if you're looking for the string
"huhhuh, inside "huhhuhhuh"
will return 1 instead of 2 (if that's what you wanted).  The fix is simple
and it is left as an excercise to the reader.

Also, I don't see how strtok() would help.

Roberto



: : > I need to know how many times a certain sustring is in a bigger string. How
: : > can I implement thi feature with a C function.
: : > TIA

: : You could achieve this with strtok(), look it up in your man pages,
: : it's probably just the thing you were looking for.

: I don't readily see how strtok() would be useful for this. strstr() in a
: loop could do it, like this:

: #include <stdio.h>
: #include <string.h>

: main(int argc, char **argv)
: {     char    *sp, *si;
:       int     len, count;

:       if (argc < 3)
:       {       printf("usage: $ %s \"substring\" \"hoststring\" [overlap]\n"
:                       , argv[0]);
:               exit(0);
:       }
:       if (argc > 3 && !strcmp("overlap", argv[3]))
:               len = 1;
:       else
:               len = strlen(argv[1]);
:       si = argv[2];
:       count = 0;
:       while (sp = strstr(si, argv[1]))
:       {       count++;
:               si = sp + len;
:       }
:       printf("%s\n occurs %d times in\n%s\n", argv[1], count, argv[2]);
:       return 0;
: }
: --

--




----------------------------------------------------------------------



Mon, 24 May 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. New to C - how to sub-string - I beg of thee for HELP

2. newbie needs help to strings, strings, strings

3. HELP:Files && Quoted Strings

4. Return string or string&

5. Getting Substring from String.LastIndexOf

6. Searching sub strings - Performance.

7. Newbie: Sub-String search and replace routine

8. Newbie: Sub-String search and replace routine

9. how to check that string contains substring?

10. Finding occurance of a substring in a string in C#

11. Searching string for substring question

12. How to do substring function for C string

 

 
Powered by phpBB® Forum Software