Compare string

W

webmaniac

Hi,

does anyone know, How to compare characters in 2 string & return How
many characters match in both String.
Like the First string is:
"Hello World"
And second is "This is a Word"
So the Function returns 3 Characters "Wor" is same in both.
 
M

Martin Rinehart

Try two boolean arrays. Initialize both to 26 'false' values. March
through the characters in the first string, setting 'true' the
appropriate array elements. Ditto for second array/second string.
'And' the two arrays. Count the 'true' values.
 
W

webmaniac

Try two boolean arrays. Initialize both to 26 'false' values. March
through the characters in the first string, setting 'true' the
appropriate array elements. Ditto for second array/second string.
'And' the two arrays. Count the 'true' values.

Hi Martin,

Can you provide some example?

Thanks
 
J

Janwillem Borleffs

webmaniac schreef:
does anyone know, How to compare characters in 2 string & return How
many characters match in both String.
Like the First string is:
"Hello World"
And second is "This is a Word"
So the Function returns 3 Characters "Wor" is same in both.

var a = "Hello World";
var b = "This is a Word";
var c = a.length > b.length ? a : b;
var ref = c == a ? b : a;
var res = '';

for (var e = '', i = -1; i < c.length; e = c.charAt(++i)) {
if (/\s/.test(e)) continue;
if (ref.indexOf(e) > -1) res += e;
}

document.write(res); // writes 'Word'


JW
 
T

Thomas 'PointedEars' Lahn

Martin said:
Try two boolean arrays. Initialize both to 26 'false' values. March
through the characters in the first string, setting 'true' the
appropriate array elements. Ditto for second array/second string.
'And' the two arrays. Count the 'true' values.

Will produce false negatives because the indexes of the substring in
question may not match (as here). Will produce false positives because
matches may not be adjacent.


PointedEars
 
T

Thomas 'PointedEars' Lahn

webmaniac said:
does anyone know, How to compare characters in 2 string & return How
many characters match in both String.
Like the First string is:
"Hello World"
And second is "This is a Word"
So the Function returns 3 Characters "Wor" is same in both.

Sounds like homework.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <4d8df00e-cbea-43b1-bf0e-c7b6c59e2f76@w3
9g2000prb.googlegroups.com>, Fri, 14 Nov 2008 06:23:48, webmaniac
Hi,

does anyone know, How to compare characters in 2 string & return How
many characters match in both String.
Like the First string is:
"Hello World"
And second is "This is a Word"
So the Function returns 3 Characters "Wor" is same in both.

Here is part of a solution for you to complete; other respondents appear
not to have understood the question - that was too difficult for
BigEars.


function DO(X1, X2) { /* fill this in */ }

S1 = "Hello banana World"
S2 = "This is a Word banana "
for (J1=0, L1=S1.length ; J1<L1 ; J1++)
for (J2=0, L2=S2.length ; J2<L2 ; J2++)
DO(S1.substr(J1), S2.substr(J2)) ;
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top