如何用pascal在两行中找到相同的单词?

时间:2014-05-03 16:50:19

标签: string pascal

我必须从键盘输入两行,而在第三行程序中必须从前两行写出相同的单词。

我该怎么办? 这是我的变种,但我找不到错误

program six;

function Slovo(s:string;n:integer):string;
  const lim =[' ',',','!'];
  var ss:string;
  k,c,i:integer;
begin
  ss:=''; c:=1; k:=1;
  while k<=length(s) do
  begin
      while not(s[k] in lim) and (k<=length(s)) do begin
        if c=n then ss:=ss+s[k];
        inc (k); end;
        inc ( c );
      while (s[k] in lim) and (k<=length(s)) do inc (k);
  end;
  Slovo:=ss;
end;

var
  s1, s2, srez : string;
  n1, n2 : integer;
  i, j : integer;

begin

  writeln (' Enter first string' );  readln (s1);
  writeln (' Enter second string' );  readln (s2);
    n1 := 1;
    n2 := 1;
    srez := ' ';
  while slovo (s1, n1) <> '' do inc (n1);
  while slovo (s2, n2) <> '' do inc (n2);
    for i := 1 to n1 do
    for j := 1 to n2 do
  if slovo (s1, i) = slovo (s2, j) then  srez := srez + slovo (s1, i) +' ';

  writeln (' string - result: ' );
  writeln (srez);

end.

1 个答案:

答案 0 :(得分:0)

我从Freepascal.org复制了它,比较了两个单词......

    Program Example66;

{ This program demonstrates the CompareText function }
{$H+}

Uses sysutils;

Procedure TestIt (S1,S2 : String);

Var R : Longint;

begin
  R:=CompareText(S1,S2);
  Write ('"',S1,'" is ');
  If R<0 then
    write ('less than ')
  else If R=0 then
    Write ('equal to ')
  else
    Write ('larger than ');
  Writeln ('"',S2,'"');
end;

Begin
  Testit('One string','One smaller string');
  Testit('One string','one string');
  Testit('One string','One string');
  Testit('One string','One tall string');
End.

这可能很有用,因为在两个单词之间是一个空格或另一个符号,如。, - ,那么您可以扫描文件或文本以查找单词并将其比较在那个例子中。 (该程序也可以升级为句子,之后它可以读取文本文件,但是它的偏离主题。)