String intersection

B

bahadir.balban

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 == s2[j])
+ return 1;
+ return 0;
+}

Thanks,
Bahadir
 
E

Eric Sosman

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 == 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);
}
 
R

regis

Eric said:
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';
}
 
C

Christopher Benson-Manica

Eric Sosman said:
#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.
 
B

Bilgehan.Balban

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.

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
 
B

Bilgehan.Balban

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
 
B

bahadir.balban

How about (untested):

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

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

Thanks,
Bahadir
 
C

Christopher Benson-Manica

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."
 
P

pete

Christopher said:
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.
 
J

James Antill

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;
}
 
P

pete

James said:
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);
}
 
M

Mark McIntyre

On Wed, 25 Jul 2007 07:22:31 -0700, in comp.lang.c ,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top