Forum > Beginners
Quick question about the string type
Thaddy:
String is not a distinct type.
It used to be shortstring, then ansistring then utf8string then utf16string.
pure pascal translates to:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program revstr;{$mode objfpc} function ReverseString(const S: String): String;var I: Integer;begin Result := ''; for I := Length(S) downto 1 do Result := Result + S[I];end; var Original, Reversed: String;begin Original := 'Hello, World!'; Reversed := ReverseString(Original); WriteLn('Original: ', Original); WriteLn('Reversed: ', Reversed);end.This should work for any string type....
[edit]most of the previous answers were correct, but did not cover "string"
wp:
--- Quote from: Thaddy on November 29, 2024, 01:54:24 pm ---This should work for any string type....
--- End quote ---
No. It will break utf8 code points.
djongepier:
Thanks again, lots of ways to achieve the same thing.
Now seeing the special 'Result' variable again reminded me of it's existence.
I'm also using a copy Lazarus within WSL2 (via fpcupdeluxe) only as a way to install the tooling, I'm using FreePascal Toolkit within VSCode to code and compile on my Work laptop.
For my personal computer running Fedora I'm also using fpcupdeluxe to get the software, but run Lazarus IDE on the desktop.
Bart:
There is a ReverseString() function in StrUtils if I'm not mistaken.
And a ReverseStringUtf8 in LazUtf8 (or maybe LazStrUtils?)
And, you can always look at The Slowest Pascal ReverseString competition, just to see how not to implement it :)
Bart
Thaddy:
Hm,
you were right about utf8.
but my example doesn't get as slow as I want it...
I should probably drop the downto... :P
Navigation
[0] Message Index
[*] Previous page