s1 - string; arr - array

 s1:=arr[i]+' '; write(f,s1); 

This is where the problem is at the end of the line is a space after the last element. How to fix?

  • the language in which he wrote - pascal - Anton Perehrest
  • for example, to make a check in the loop - if not the last element - add a space .... or delete the last element from the line from the line ... trim - Alex Shimansky
  • If you are given an exhaustive answer, mark it as accepted. - Suvitruf

2 answers 2

If the array indices start at 1, then fix it like this:

 s1 := ''; for i := 1 to arrLength - 1 do // добавим все, кроме последнего, ... s1 := s1 + arr[i] + ' '; // ... с пробелом s1 := s1 + arr[arrLength]; // добавим последний без пробела write(f, s1); // запишем результат 

    Inside the loop, check whether the last element is or not:

     for i := 1 to arrLength do begin if i < arrLength then s1:=arr[i] + ' '; else s1:=arr[i]; end 
    • You can shorten ... write s1:=arr[i]; in any case ... but below write if i < arrLength then s1:= s1 + ' '; - Alexey Shimansky
    • Try to write more detailed answers. You can explain the solution, point out specific errors, provide links with a more detailed analysis of the problem, etc. - Athari
    • Checking the conditions inside the loop, which will work once at the end, is a useless slowdown of the program. But if your goal is to heat the room where the computer is located, then this is exactly what you need =) - kot-da-vinci