Hello. can You help me with my program. I have to enter two lines from the keyboard, and in the third line program must write the same words from the first two lines. I also can use standart Pascal functions.

But in this code I use my own function and I cannot find a mistake.

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.

Thanks

What problems are you having?

One issue is that n1 and n2 are set to the wrong values. Suppose S1 has 3 words: 'one two three'. Your logic in this line:

while slovo (s1, n1) <> '' do inc (n1);

is like this:
(n1 was originally set to 1).
slovo returns a non-empty string 'one' so increment n1 to 2.
slovo returns a non-empty string 'two' so increment n1 to 3
slovo returns a non-empty string 'three' so increment n1 to 4
slovo returns an empty string so end the loop.

Now n1 has the value 4 but I think you want it to be 3, the number of words in s1.
Then later your line:

for i := 1 to n1 do

will mean you do the loop 4 times not 3.
The same happens for n2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.