Board index » delphi » Comparing strings char by char

Comparing strings char by char

hey guys, a little help would be appreciatted
i have 16 strings, 30 characters each
say i then get another string
how would i go about comparing this new string to the 16 character by
character, and finding which it is most similar to?
thanks,
Leesa.
 

Re:Comparing strings char by char


In article <3cf5ddde$0$31824$afc38...@news.optusnet.com.au>, "leesa" <biteme>
writes:

Quote
>i have 16 strings, 30 characters each
>say i then get another string
>how would i go about comparing this new string to the 16 character by
>character, and finding which it is most similar to?
>thanks,

Use a Soundex function which enables you to check for similar sounding names.
Otherwise you have to define "most similar to" before you can start to check.

Alan Lloyd
alangll...@aol.com

Re:Comparing strings char by char


Quote
"leesa" <biteme> wrote in message

news:3cf5ddde$0$31824$afc38c87@news.optusnet.com.au...

Quote
> hey guys, a little help would be appreciatted
> i have 16 strings, 30 characters each
> say i then get another string
> how would i go about comparing this new string to the 16 character by
> character, and finding which it is most similar to?
> thanks,

Store the 16 strings in a string list. Loop through the list calling a
function that returns a value between 0 and 100, where 100 means identical
strings.

var Master : tStringList;

function MostSimilar (const aString : string) : string;

    function HowSimilar (const A, B : string) : integer;

    begin
    // return value between 0 and 100, where 100 means A = B
    end;

var ix, aVal, bestVal, best : integer;

begin
best := 0;
bestVal := 0;
for ix := 0 to (Master.Count - 1) do
    begin
    aVal := HowSimilar (aString, Master [ix]);
    if aVal > bestVal
    then begin
        bestVal := aVal;
        best := ix;
        end;
    end;
if (Master.Count > 0)
then result := Master [best]
else result := '';
end;

How to determining similarity is not an easily answered question, especially
given the lack of detail. Are all the words in the same language? Are all
the words from a particular domain, e.g. proper names, scientific terms,
verbs? What constitutes similarity, i.e. same root different conjugations,
similar meaning, similar spelling? Etc.

Other Threads