0 Members and 3 Guests are viewing this topic.
//-----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;
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;
//-----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 strictfunction 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".
Quote from: rhide on January 18, 2008, 02:51:47 pm1. 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
Result := '';for i := length(Text) downto 1 do result := result + Text[i];
Quote from: Kavukamari on January 18, 2008, 06:52:33 pmQuote from: rhide on January 18, 2008, 02:51:47 pm1. 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 charsyes but with recursion it will keep doing it until the whole string is reversedi.e.if you have the string "cool"you call RevStr("cool")it looks at the string which is bigger than 1then 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 oneso 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 callsreturn 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??)
copy(String, start, number of chars)