For Programmers: Free Programming Magazines  


Home > Archive > C > July 2007 > String intersection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author String intersection
bahadir.balban@gmail.com

2007-07-25, 7:56 am

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

Eric Sosman

2007-07-25, 7: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
Spoon

2007-07-25, 7:56 am

Eric Sosman wrote:

> Bahadir Balban wrote:
>
>
> What's this?


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

http://lxr.linux.no/source/include/linux/string.h
regis

2007-07-25, 7:56 am

Eric Sosman wrote:
> bahadir.balban@gmail.com wrote:
>
>
> 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';
}

Christopher Benson-Manica

2007-07-25, 6:57 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.
Bilgehan.Balban@gmail.com

2007-07-25, 6:57 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

Bilgehan.Balban@gmail.com

2007-07-25, 6:57 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

bahadir.balban@gmail.com

2007-07-25, 6:57 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

Christopher Benson-Manica

2007-07-25, 6:57 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.
pete

2007-07-25, 9:56 pm

Christopher Benson-Manica wrote:
>
> Bilgehan.Balban@gmail.com wrote:
>
>
> 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
James Antill

2007-07-26, 6:57 pm

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/
pete

2007-07-26, 6:57 pm

James Antill wrote:
>
> On Wed, 25 Jul 2007 07:31:08 -0400, Eric Sosman wrote:
>
>
> 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
Mark McIntyre

2007-07-26, 6:57 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:
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com