Link to home
Start Free TrialLog in
Avatar of edeaux
edeaux

asked on

String comparison

I need a report using html that will show the difference of a sentence to another sentence quickly. The difference (by character) will display in red color so need to add <font tag>. Anybody here knows how to do it?

example:
  Right Sentence: The ball is bouncing.
  Sentence entered by a user: The ball is bounsing.

So letter "s" in bounsing entereed by a user will be displayed in red.


thanks,

edwin
Avatar of mokule
mokule
Flag of Poland image

And should be what in
Th ball is bouncing
or
The    ball    is bouncing
or
The ball is dgfdfgdsfgsdf
You could load both sets of data into two stringlists and loop through the strings.

StringList1.LoadFromFile('c:\somepath\somefile')
StringList2.LoadFromFile('c:\somepath\somefile')

Grab each line from the stringlist into 2 temporary strings and then loop through character by character
for i := 0 to Pred(StringList1.Count) do
begin
  sTemp1 := StringList1.Strings[i];
  sTemp2 := StringList2.Strings[i];

  for j := 1 to Length(sTemp1) do
  begin
    if sTemp1[j] <> sTemp2[j] then
      // add your extra font tags

  end
end;

This is just a rough guide but should be able to help you on your way I think
Avatar of Ashok
procedure TForm1.Button1Click(Sender: TObject);
var
  sC, sW: String;
  i : Integer;
begin
  sC := 'The ball is bouncing';
  sW := 'The ball is bounsing';
  for i := 1 to Length(sC) do
  begin
    if sC[i] <> sW[i] then
    begin
      // change font color of wrong character
      ShowMessage('wrong ' + sW[I]);
    end;
  end;
end;

HTH
Ashok
You have to also take into consideration, spaces, I guess.

The ball is bouncing
The ball             is bouncing

Even if you make the blanks into BOLD or RED, your user can't see it. So I guess it's best you clearly define how are you going to match the differences first :-)
procedure TForm1.Button1Click(Sender: TObject);
var                      // fuction TForm1.Compare2Strings(sC, sW : String);
  sC, sW: String;  // you can move these 2 variables in function declaration, and make new function shown in above comment line
  i : Integer;
begin
  sC := 'The ball is bouncing';
  sW := 'The ball is bounsing';
  if sC <> sW then
  for i := 1 to Length(sC) do
  begin
    if sC[i] <> sW[i] then
    begin
      // change font color of wrong character
      ShowMessage('wrong ' + sW[I]);
    end;
  end;
end;

HTH
Ashok
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
edeaux,

Has anyone answered you question correctly?

Ashok
edeaux,

Has anyone answered your question correctly?

Ashok