1

we are doing pascal in school and task is to get all words with only latin chars which have descendingly alphabetical order in those words. So I input "sba dcb onml dfsf cba kl2 joh;" and programm has as output "sba dcb onml dfsf cba" which is mostly right but I cant udnerstand why it contains "dfsf" and how can I fix it? P.S.: Sorry for my english.

    var
    u: string;
    ws: array[1..100] of string;
    w: string;
    len: integer;
    i, j, q, t, a, b, n, s, z: integer;
begin
    writeln('type string:');
    read(u);
    len := length(u);
    a := 1;
    i := 1;
    s := 1;
    while i <= len do
        if (u[i] >= '!') and (u[i] <= '~') then begin
            w := u[i];
            i := i + 1;
            while (i <= len) and
            ((u[i] >= '!') and
            (u[i] <= '~')) do begin
                w := w + u[i];
                i := i + 1;

                            for t := 1 to length(w) do begin
                              if not (w[t] in ['a'..'z']) then begin
                                s:= 0;
                                Break;
                              end else begin
                              s:= s + 1;

                                n:=length(w);
                                for a:=1 to n-1 do
                                for b:=a+1 to n do
                                  if not (w[a] >= w[b]) then
                                  begin
                                    s:=0;
                                  end else begin
                                  s:= s + 1;
                                  end;

                              end;
                            end;




            end;


                if s<>0 then begin
                q:= q + 1;
                ws[q] := w;
                end;
        end
        else
            i := i + 1;

    for i := 1 to q do
        writeln(ws[i]);

end.
Aura
  • 11
  • 4
  • Not an answer, but how about properly formatting your code? It is almost unreadable. You may also want to cut it up into procedures and functions, to make it even more readable. Readability is important. – Rudy Velthuis Feb 25 '18 at 13:03
  • And meaningful variable names also help reading the code. If you put it aside and have to read it again, after say 3 months, you will have big problems understanding it. – Rudy Velthuis Feb 25 '18 at 13:24
  • Yeah you are right, I'm sorry. I got lot to learn. – Aura Mar 03 '18 at 09:28

1 Answers1

0

Realized that part is increasing variable (s) any time when there is pair of descendingly alphabetical chars so I just removed it.

else begin
s:= s + 1;
end;
Aura
  • 11
  • 4