String intersection

  • Follow


Hi,

How do you check string intersection? There doesn't seem to be a
library function to do that, e.g:

+#include <linux/string.h>
+
+/* AND'ing two strings (checks intersection) */
+static int strand(char *s1, char *s2)
+{
+	int i, j;
+	int slen1 = strlen(s1);
+	int slen2 = strlen(s2);
+
+	for(i = 0; i < slen1; i++)
+		for(j = 0; j < slen2; j++)
+			if (s1[i] == s2[j])
+        		        return 1;
+    return 0;
+}

Thanks,
Bahadir

0
Reply bahadir.balban (14) 7/25/2007 11:18:56 AM

bahadir.balban@gmail.com wrote:
> Hi,
> 
> How do you check string intersection? There doesn't seem to be a
> library function to do that, e.g:
> 
> +#include <linux/string.h>

     What's this?

> +/* AND'ing two strings (checks intersection) */
> +static int strand(char *s1, char *s2)
> +{
> +	int i, j;
> +	int slen1 = strlen(s1);
> +	int slen2 = strlen(s2);
> +
> +	for(i = 0; i < slen1; i++)
> +		for(j = 0; j < slen2; j++)
> +			if (s1[i] == s2[j])
> +        		        return 1;
> +    return 0;
> +}

     How about (untested):

	#include <string.h>
	static int strand(const char *s1, const char *s2) {
	    return strcspn(s1, s2) < strlen(s1);
	}

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
Reply esosman2 (2945) 7/25/2007 11:31:08 AM


Eric Sosman wrote:

> Bahadir Balban wrote:
> 
>> How do you check string intersection? There doesn't seem to be a
>> library function to do that, e.g:
>> 
>> +#include <linux/string.h>
> 
>     What's this?

It looks like a non-standard include directive preceded by a '+'

http://lxr.linux.no/source/include/linux/string.h
0
Reply Spoon 7/25/2007 12:31:01 PM

Eric Sosman wrote:
> bahadir.balban@gmail.com wrote:
> 
>>Hi,
>>
>>How do you check string intersection? There doesn't seem to be a
>>library function to do that, e.g:
>
>      How about (untested):
> 
> 	#include <string.h>
> 	static int strand(const char *s1, const char *s2) {
> 	    return strcspn(s1, s2) < strlen(s1);
> 	}
> 

#include <string.h>
static int strand(const char *s1, const char *s2) {
    return s1 [strcspn(s1, s2)] != '\0';
}

0
Reply regis (23) 7/25/2007 12:42:25 PM

Eric Sosman <esosman@ieee-dot-org.invalid> wrote:

>         #include <string.h>
>         static int strand(const char *s1, const char *s2) {
>             return strcspn(s1, s2) < strlen(s1);
>         }

Besides the fact that the function name is in the implementation's
namespace (you know this, but OP doesn't), seems like a big
improvement to me.  I can't think of an obvious use-case for the
function, however.

-- 
C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.
0
Reply ataru1 (106) 7/25/2007 1:18:21 PM

On 25 Jul, 14:18, Christopher Benson-Manica
<at...@faeroes.freeshell.org> wrote:
> Besides the fact that the function name is in the implementation's
> namespace (you know this, but OP doesn't),

 What do you mean? Do you mean strand() is in string.h namespace?

> seems like a big
> improvement to me.  I can't think of an obvious use-case for the
> function, however.
>
> --
> C. Benson Manica           | I *should* know what I'm talking about - if I
> cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

I want to know if a string contains a set of characters. For example
when I receive an input string, I want to validate that it has
alphanumeric letters, or numbers. Is there any better way?

Thanks,
Bahadir

0
Reply bilgehan.balban (64) 7/25/2007 2:22:31 PM

On 25 Jul, 15:22, Bilgehan.Bal...@gmail.com wrote:
> I want to know if a string contains a set of characters. For example
> when I receive an input string, I want to validate that it has
> alphanumeric letters, or numbers. Is there any better way?
>
> Thanks,
> Bahadir

Sorry, what I meant was, to validate that input contains *at least*
one alphanumeric character, or say, a number.

Thanks,
Bahadir

0
Reply bilgehan.balban (64) 7/25/2007 2:25:24 PM

On 25 Jul, 12:31, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>      How about (untested):
>
>         #include <string.h>
>         static int strand(const char *s1, const char *s2) {
>             return strcspn(s1, s2) < strlen(s1);
>         }
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid

Actually, strpbrk() seems to do the same thing, only that it returns
the occuring character rather than a zero or one.

Thanks,
Bahadir

0
Reply bahadir.balban (14) 7/25/2007 2:35:17 PM

Bilgehan.Balban@gmail.com wrote:

>  What do you mean? Do you mean strand() is in string.h namespace?

Yes. n869, 7.26.11 says "Function names that begin with str [or some
other prefixes] and a lowercase letter ... may be added to the declarations
in the <string.h> header."

-- 
C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.
0
Reply ataru2 (241) 7/25/2007 4:09:13 PM

Christopher Benson-Manica wrote:
> 
> Bilgehan.Balban@gmail.com wrote:
> 
> >  What do you mean? Do you mean strand() is in string.h namespace?
> 
> Yes. n869, 7.26.11 says "Function names that begin with str [or some
> other prefixes] and a lowercase letter ...
> may be added to the declarations in the <string.h> header."

And there's also:
       7.26.10  General utilities <stdlib.h>
       [#1] Function names that begin  with  str  and  a  lowercase
       letter  (possibly  followed  by  any  combination of digits,
       letters, and underscore) may be added to the declarations in
       the <stdlib.h> header.

And let's not forget

       7.26  Future library directions
       [#1]  The  following  names  are  grouped  under  individual
       headers for convenience.  All external names described below
       are  reserved  no  matter  what  headers are included by the
       program.

-- 
pete
0
Reply pfiland (6614) 7/26/2007 12:15:05 AM

On Wed, 25 Jul 2007 07:31:08 -0400, Eric Sosman wrote:

>      How about (untested):
> 
> 	#include <string.h>
> 	static int strand(const char *s1, const char *s2) {
> 	    return strcspn(s1, s2) < strlen(s1);
> 	}

 Much faster would be:

 	static int my_strand(const char *s1, const char *s2)
        {
            const char *end = s1 + strcspn(s1, s2);
 	    return !!*end;
 	}

-- 
James Antill -- james@and.org
C String APIs use too much memory? ustr: length, ref count, size and
read-only/fixed. Ave. 44% overhead over strdup(), for 0-20B strings
http://www.and.org/ustr/
0
Reply james-netnews (137) 7/26/2007 4:57:39 PM

James Antill wrote:
> 
> On Wed, 25 Jul 2007 07:31:08 -0400, Eric Sosman wrote:
> 
> >      How about (untested):
> >
> >       #include <string.h>
> >       static int strand(const char *s1, const char *s2) {
> >           return strcspn(s1, s2) < strlen(s1);
> >       }
> 
>  Much faster would be:
> 
>         static int my_strand(const char *s1, const char *s2)
>         {
>             const char *end = s1 + strcspn(s1, s2);
>             return !!*end;
>         }

#include <string.h>

#define my_strand(s1, s2)    (strpbrk((s1), (s2)) != NULL)

static int (my_strand)(const char *s1, const char *s2)
{
    return my_strand(s1, s2);
}

-- 
pete
0
Reply pfiland (6614) 7/26/2007 6:13:09 PM

On Wed, 25 Jul 2007 07:22:31 -0700, in comp.lang.c ,
Bilgehan.Balban@gmail.com wrote:

>On 25 Jul, 14:18, Christopher Benson-Manica
><at...@faeroes.freeshell.org> wrote:
>> Besides the fact that the function name is in the implementation's
>> namespace (you know this, but OP doesn't),
>
> What do you mean? Do you mean strand() is in string.h namespace?

Function names that begin with str, mem, or wcs and a lowercase letter
are reserved for future library direction. (see 7.26.11 of the ISO
standard).

Which means you may not write your own function called str[a-z]...
-- 
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place. 
 Therefore, if you write the code as cleverly as possible, you are, 
 by definition, not smart enough to debug it."
--Brian Kernighan
0
Reply markmcintyre (4547) 7/26/2007 10:39:31 PM

12 Replies
53 Views

(page loaded in 0.12 seconds)

Similiar Articles:















7/18/2012 9:13:19 AM


Reply: