Author Topic: a bit of help needed  (Read 1223 times)

0 Members and 1 Guest are viewing this topic.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
a bit of help needed
« on: January 18, 2008, 12:52:03 pm »
Code: [Select]
//-----ReverseString-----
//Notes: reverses a string (right side goes left, left goes right)
//whatever, have fun...

function RevStr(Text: string):string;
var
  i, j: integer;
begin
//  Result:=Text;
  j:=length(Text);
  i:=0;
  While (j > 0) and (i < Length(Text)) do begin
    Result[i]:=Text[j];
    dec(j,1);
    inc(i,1);
  end;
end;

I'm pretty sure I could get this to work, but anything I change regarding i and j does not help (changing the while line doesnt help either)

am I missing something?
« Last Edit: January 18, 2008, 12:56:35 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: a bit of help needed
« Reply #1 on: January 18, 2008, 01:48:45 pm »
I would do it with recursion, if the strings are not text books it works fine and is a lot simpler
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: a bit of help needed
« Reply #2 on: January 18, 2008, 01:54:22 pm »
recursion? what is that? and what about text books?
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: a bit of help needed
« Reply #3 on: January 18, 2008, 02:51:47 pm »
Hm.. alright, sorry for being somewhat short on you, I'll explain:
Basicly, recursion is functions that call themselves from themselves. The whole concept could be somewhat hard to get a hold of in the beginning, but when you understand it the power of it is great!

Lets jump right on to your problem as an example:
Code: [Select]
function RevStr(Text : string) : string;
Begin
   if length(Text) <= 1 then
      Result := Text
   else
      Result := Text[length(Text)] + RevStr(copy(Text,2,length(Text)-2)) + Text[1];
End;

So what happened here?
1. If there is only 1 char, or the string is empty we return the string.
2. If not: return "Last char" + RevStr(The string without first and last char) + "First char".

The power of recursion is that this works! We just need to think it, write it - and we're done. The computer saves all return calls in memory, and when it meets a stop (in this case, when there is only 1 or no chars left) it terminates safely.
This uses up somewhat much memory, so dont send textbooks to it, or the computer will complain about filling its memory up
« Last Edit: January 18, 2008, 02:57:57 pm by rhide »
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: a bit of help needed
« Reply #4 on: January 18, 2008, 03:24:40 pm »
Unfortunately, you can not do prototyping (already asked) which i think would basically create the function without saying what is inside it yet... so you can call it before it is actually declared.

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: a bit of help needed
« Reply #5 on: January 18, 2008, 03:41:28 pm »
Yes Ive tried that, and its unfortunate :( However, that has absolutely nohing to do with recursion...
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: a bit of help needed
« Reply #6 on: January 18, 2008, 06:52:33 pm »
while we're at it, theres nothing wrong with this one, except it includes the end "bracket" in the result:

Code: [Select]
//-----ParamChk-----
//Notes: used for easier commands, instead of a set amount of specific
//params, you add tags and put the values between them (like a '-?' shortcut parameter)

//this uses a start tag (-?) and an end tag (?-) just a bit harder to use, but not as strict

function ParamChk(Text: string; OVar, CVar: string): string;
begin
  if ContainsString(Text, OVar) and ContainsString(Text, CVar) then begin
    Result:=Copy(Text,Pos(OVar,Text)+Length(OVar),Pos(CVar,Text)-Length(CVar));
  end;
end;

Function OnCommand(ID: byte; Text: string):boolean;
begin
  Result:=false;
  if GetPiece(Text, ' ', 0) = '/hi' then WriteConsole(ID,ParamChk(Text,'-p','p-'),$FF20FF20);
end;


1. If there is only 1 char, or the string is empty we return the string.
2. If not: return "Last char" + RevStr(The string without first and last char) + "First char".


actually, I wanted to reverse the entire string, not just first & last chars
« Last Edit: January 18, 2008, 07:05:42 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline saintpoida

  • Major(1)
  • Posts: 4
Re: a bit of help needed
« Reply #7 on: January 18, 2008, 10:45:37 pm »


1. If there is only 1 char, or the string is empty we return the string.
2. If not: return "Last char" + RevStr(The string without first and last char) + "First char".


actually, I wanted to reverse the entire string, not just first & last chars

yes but with recursion it will keep doing it until the whole string is reversed

i.e.

if you have the string

"cool"

you call RevStr("cool")
it looks at the string which is bigger than 1
then swaps 'c' and 'l' and runs itself again with the middle letters
'l'+RevStr("oo")+'c'
so this run through its now checking "oo", its bigger than one
so it looks like this inside the second version of itself
'o'+RevStr("")+'o' 

after it runs into a string of 1 or less  it will start exiting its own calls
return each string as it goes giving you

'l'+'o'+'o'+'c'

thats my understanding anyway....

hope its right

(note: looking at it and not knowing pascal that well and not knowing what copy returns it looks as if you would get an extra letter out of the last call rhide??)
« Last Edit: January 18, 2008, 10:49:00 pm by saintpoida »

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: a bit of help needed
« Reply #8 on: January 19, 2008, 02:48:31 am »
Or you should be able to do it as:

Code: [Select]
Result := '';
for i := length(Text) downto 1 do
  result := result + Text[i];

I haven't tested to see if it works or not, but aside from errors it should work.

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: a bit of help needed
« Reply #9 on: January 19, 2008, 04:02:52 am »


1. If there is only 1 char, or the string is empty we return the string.
2. If not: return "Last char" + RevStr(The string without first and last char) + "First char".


actually, I wanted to reverse the entire string, not just first & last chars

yes but with recursion it will keep doing it until the whole string is reversed

i.e.

if you have the string

"cool"

you call RevStr("cool")
it looks at the string which is bigger than 1
then swaps 'c' and 'l' and runs itself again with the middle letters
'l'+RevStr("oo")+'c'
so this run through its now checking "oo", its bigger than one
so it looks like this inside the second version of itself
'o'+RevStr("")+'o' 

after it runs into a string of 1 or less  it will start exiting its own calls
return each string as it goes giving you

'l'+'o'+'o'+'c'

thats my understanding anyway....

hope its right

(note: looking at it and not knowing pascal that well and not knowing what copy returns it looks as if you would get an extra letter out of the last call rhide??)

Yes, that is exactly how it works :) About your last question: Yes it's entirely correct. The copy function looks like this:
Code: [Select]
copy(String, start, number of chars)So we want to copy(Text, 2, length(Text)-2)
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline saintpoida

  • Major(1)
  • Posts: 4
Re: a bit of help needed
« Reply #10 on: January 19, 2008, 08:17:40 am »
chrisgbk, nice that looks like it would work and is much more compact

rhide, cool yeah i wasnt sure but awesome!!