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: How to intersect cell arrays? - comp.soft-sys.matlab... intersect(c{1},c{2}) I get the output 0123456789 I want the result of intersection of ... and some numbers all mixed up. How can i convert each of my cells to a string? Calling a method - comp.lang.java.help... deleteValue); } System.out.print("Enter 'u' to find the union\nEnter 'i' to find the intersection\n" + "Enter 'c' to compare the sets\nIf not enter 'n'"); String ... Merge Faces configs - comp.cad.solidworksor Select a face of the flattened part insert a sketch use the intersection curve tool ... (SW still seems to rely somewhat on the magic "FLAT-PATTERN" string to identify ... bounding box algorithm - comp.graphics.api.openglIntersection of 3 spheres in PSTricks - comp.text.tex Smallest circle covering set of ... bounding box by hand is quite annoying, especialy if ... convert an std::string to ... How do you plot opaque circles with error bars? - comp.soft-sys ...(Imagine making a crosshairs by laying two strings of black licorice on a table at right angles, and then laying a white cookie on top of the intersection. List Files By Date Range - comp.unix.programmer... also 'find ... \! -newer <newest-file>' and then using 'comm' to find the intersection ... HELP - Moving Files By Date Range - comp.unix.admin In this case the strings limit ... difference between objective and fitness function(s)? - comp.ai ...So would that be the bit string we are aiming to evolve? And the fitness is the ... between the two regression lines ... > how can I find a solution about intersection point ... Minimum Phase Impulse Response - comp.dsp... This normal also intersects the parallel through D. Designate the intersection point ... of my first attempt, I devised a way to trisect the angle with a piece of string ... plot function with 3 variables - comp.soft-sys.matlab... now i want to plot these functions in matlab and find an idea about intersections ... Concatenate string with variable - comp.soft-sys.matlab Hello I would like to put ... Colors for JTable cells - comp.lang.java.gui... string "true", it could be rendered as a * string or ... A JTable consists of rows and columns, and the intersection of each is ... EXPORTING XYZ POINTS COORDINATES INTO EXCEL - comp.cad.solidworks ...... Dim sp As SldWorks.SketchPoint Dim ep As SldWorks.SketchPoint Dim s As String Dim ... tube bending help!!!! - comp.cad.solidworks It requires input of x, y, & z intersection ... Gradebook template - comp.databases.filemaker... class ('Classes.fp5'), class assignment ('Assignements.fp5'), which is an intersection ... Filemaker's 20/60 rule for relational keys: only 60 characters, in strings no ... My Tech Post: Find the intersection of Two StringsRecently I was asked this question in an Interview... Thought this would help others who read my blog.. Problem Find the intersection of two strings... Intersection between two lists of stringsI have two lists defined as : List<string> list1 = new List<string>(); List<string> list2 = new List<string>(); I have added items to both the list. Now I ... 7/18/2012 9:13:19 AM
|