okay, i've been looking for a nice algorithm for this one for a long time, all i find are cpp, java and c# code. i've been trying to reverse the order of each word in its place, but i don't seem to be able to do so. i tried with two strings, swapping the chars of each word but it simply didn't work...
this is my...attempt to write each word backwards...

program stringzorz;

var
s1, s2 : string;
i, j ,k, start, endd : integer;

procedure swp(var c1, c2 : char);
          var p : char;
          begin
               p := c1;
               c1 := c2;
               c2 := p;
          end;

begin
     readln(s1);
     s2 := s1;
     endd := 1;
     i := 1;
     while i < length(s1) do
     begin
     while s1[i] <> ' ' do
           i:= i + 1;
     start := i;
     j := i;
     for i := endd to start do
         begin
              swp(s1[i], s2[j]);
              j := j - 1;
         end;
     end;
     writeln(s1);

end.

i know it's all...scrambled, i'm not that good at the esthetic part of coding but could someone please give me an idea atleast?

umm, the thread can be removed...or saved...i've found a better way of reversing a sentence by deleating :

program stringzorz;

var
s : string;
i, j, k : integer;

begin
     readln(s);
     insert(' ', s, 1);
     while length(s) > 1 do
           begin
                i := length(s);
                while s[i] <> ' ' do
                      i := i - 1;
                for j := i+1 to length(s) do
                    write(s[j]);
                delete(s, i + 1,  length(s) - i + 1);
                while s[i] = ' ' do
                      begin
                           if i = 1 then break;
                           write(s[i]);
                           i := i - 1;
                      end;
                delete(s, i + 1, length(s) - i + 1);
           end;
      readln;
end.

For what its worth, here is my solution:

VAR

s, t, w : string;
i : integer;

BEGIN

readln(s);
w := '';
t := '';


FOR i := 1 TO length(s) DO
  IF s[i] <> ' ' THEN
    w := w + s[i]
  ELSE IF length(w) > 0 THEN BEGIN
    t := w + ' ' + t;
    w := '';
  END;

IF length(w) > 0 THEN
  t := w + ' ' + t;

If I had to do it in-place, I would have just reversed the entire string, then reversed each word in the string...

But you did a good job with your temp strings. Nice!

im facing the same problem like this. my boss like to know how to reverse word in a sentence but you can only use 1 byte buffer (byte or char). could you help me? TIA

if the input is i love you, it should be you love i. and the long of the string isnt defined...

You'll need an index or pointer to the last character in the string, and an index or pointer to the first character in the string, and a temporary char. Then swap values, dec( last ), inc( first ) and repeat until last <= first.

Do the same thing for each word.

Good luck!

brilliant!!

thanks:)

but i would like to add that the last index/pointer need to find a space character before doing the swap proccess and then go back to an earlier word...:)

the rest would be the looping proccess:)

i found a simpler way to do it.. :)

like this :

input : im visiting your home
proccess : change the string upside down : emoh ruoy gnitisiv mi
do the 2nd process : turn it again by the words : home your visiting im

voila... we can do it with 1byte buffer much easier...

Just another way to do this - introducing recursivity:

function Reverse(var value :string; start :integer) :string;
var
  StrTmp :string;
begin
  if start <= length(value) then
  begin
    StrTmp := '';
   // getting the word
    while (start <= length(value)) and
          (value[start] <> ' ') do
    begin
      StrTmp := StrTmp +value[start];
      Inc(start);
    end;
  // preserve spaces and put it before the word
    while value[start] = ' ' do
    begin
      StrTmp := ' ' +StrTmp;
      Inc(start);
    end;
  // recursive call to get the next word and insert it before the actual - StrTmp
    Reverse := Reverse(value, start) +StrTmp;
  end else
    ReverseWord := '';
end;

var
  sl :string;
begin
  readln(sl);
  sl := Reverse(sl, 1);
  writeln(sl);
end.

Bye

var St,StReverse:String; m,n:Integer;
begin
  St:='I need to reverse this sentence';
  StReverse:='';
  n:=Length(St)+1;
  repeat
    m:=n;
    repeat dec(n); until (St[n]=' ') or (n=0);
    StReverse:=StReverse+' '+Copy(St,n+1,m-n-1);
  until n=0;
  Label1.Caption:=St+':'+StReverse;
end;

You can get the input string/strings from anywhere.
This was simply to show you another way!

This article has been dead for over six months. Start a new discussion instead.