It is necessary to swap the first and last letters of each word. Help me please. Half a day I suffer, but reached only:

Var i,t,p: Integer; n:byte; s,z,c: String; Begin writeln('Vvelite stroku'); readln(s); n:=length(s); p:=Pos(' ',S); if p<>0 then begin z:=s[1]; c:=copy(s,p-1,1); delete(s,p-1,1); insert(z,s,p-1); delete(s,1,1); insert(c,s,1); end; 

How to continue to be?

  • one
    Judging because you already have, you do not fully understand what is happening in your code. - andrybak

2 answers 2

First, you need a cycle in which you search for the index of the whitespace character. In addition, think about the fact that not only a space can be at the end of a word, but several other different signs. For your task, using the Pos function does not make much sense. For example,

 for I := 1 to length(s) do begin if (s[i] = ' ') or (s[i] = ',') or ... then ... end; 

Secondly, the use of the functions copy, insert, delete is also meaningless. Since the string is represented by an array of characters, you only need to change the values ​​by the index of the first letter of the word and your current index in the search minus one. In this way:

 var c: char; firstLetter: integer;//в начале работы программы должна быть = 1 //(самому первому символу строки) ...//нашли пробел/другой символ c = s[firstLetter]; //сохраняем первую букву слова s[firstLetter] := s[i - 1]; //перезапиписываем первую букву последней s[i - 1] := c; //перезаписываем последнюю букву //сохраняем первую букву следующего слова, //не забыв проверить не вышли ли мы за пределы длины строки if (i + 1) <= length(s) then //если i + 1 будет больше, мы и так из цикла выйдем firstLetter = i + 1; 

All this can still be flavored with checks of various kinds, when there are several spaces in a row, for example. Be careful with indexes, I could miss something somewhere, because I described just one of the solutions.

  • The pos , insert , delete and copy functions are in Pascal. - andrybak
  • !!!!!!!!! firstLetter: = 1 - December-December
  • What does the debugger say? What is i equal to during an error? - Dex
  • BUT if you initially set that i: = 2 does not give an error, but does not display an answer - December-December
  • I wrote that with i := 1 everything works fine. Did you use the debugger? - Dex

So you swap the first and the last letter of the first word in the line (if the line does not begin with a space).

To do this with all the words, you need to organize a cycle by the condition "as long as there is an unhandled word in the string". But do not do that.

It is easier to go through the entire line in one cycle and collect the words by the symbol by hand.

  • By condition, all words are separated only by a space and only by one. There is a problem: s [firstLetter]: = s [i - 1]; a message appears on this line - "The index was outside the bounds of the array", the program is terminated. and this message appears every time with similar records s [f]: = [i + 1], s [f]: = [i-1], etc. - april-December
  • Unfortunately, this did not change anything. Runtime Error: The index was outside the bounds of the array. - april-December
  • Everything works (rented previous comment). Look at the indexes, look for where you made a mistake. It's not difficult for me to lay out the working code, but I have already laid it out above, I just checked it out also. - Dex
  • I run through pascalabc, well, I recalibrated everything, I even understand, ideally, the code. but on the line s [firstLetter]: = s [i - 1]; the whole issue is issued to the very Oshiyuku ( - april-kadabryaba
  • sorry * mistake - December-December