Here is my attempt to solve the About.com Delphi challenge to un-camel-case a string.
unit challenge1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
check = 65..90;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var s1,s2 :string;
int : integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
checks : set of check;
begin
s1 := edit1.Text;
for i := 1 to 20 do
begin
int :=ord(s1[i]) ;
if int in checks then
insert(' ',s1,i-1);
end;
showmessage(s1);
end;
end.
check is a set that contains capital letters so basically whenever a capital letter is encountered the insert function adds space before its encountered (inside the s1 string), but my code does nothing. ShowMessage just shows text as it was entered in Edit1. What have I done wrong?
You’re correct that
checkis a set, but you haven’t assigned any value to it yet, so its value is indeterminate. There are not the characters you expect in it, so theintest probably always fails. (Didn’t the compiler warn you that you hadn’t assigned anythingcheckyet?)You don’t really want to define
checkas a subrange type. Rather, you should replacecheckwith the built-inTSysCharSettype, which is a set of characters. Then assignchecklike this:Furthermore, rather than check the numeric value of the string with
intandOrd, just use theCharvalues directly:if s1[i] in check. You’ll also want to use theLengthfunction so you process the entire string instead of assuming that the input will always be exactly 20 characters long. You’ll also want to store the result into something other thans1since, as Caldon points out, you don’t want to modify it at the same time that you’re still reading more characters from it.