这段代码对字符串进行非驼峰式大小写有什么问题?
这是我尝试解决About.com Delphi对联合国的挑战-驼峰式大小写字符串。
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
是一个包含大写字母的集合,因此基本上每当遇到大写字母时,插入函数都会在遇到大写字母之前添加空格(在 s1
字符串内),但我的代码不执行任何操作。 ShowMessage
仅显示在 Edit1
中输入的文本。我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
check
是一个集合,这是正确的,但您尚未为其分配任何值,因此它的值是不确定的。其中没有您期望的字符,因此in
测试可能总是失败。 (编译器没有警告您尚未分配任何check
吗?)您并不真的想将
check
定义为子范围类型。相反,您应该将check
替换为内置的TSysCharSet
类型,这是一组字符。然后像这样分配check
:此外,不要使用
int
和Ord
检查字符串的数值,只需使用Char
直接取值:if s1[i] in check
。您还需要使用Length
函数,以便处理整个字符串,而不是假设输入始终正好是 20 个字符长。您还需要将结果存储到s1
以外的内容中,因为 Caldon 指出,您不想在修改它的同时仍在从中读取更多字符。You're correct that
check
is 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 thein
test probably always fails. (Didn't the compiler warn you that you hadn't assigned anythingcheck
yet?)You don't really want to define
check
as a subrange type. Rather, you should replacecheck
with the built-inTSysCharSet
type, which is a set of characters. Then assigncheck
like this:Furthermore, rather than check the numeric value of the string with
int
andOrd
, just use theChar
values directly:if s1[i] in check
. You'll also want to use theLength
function 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 thans1
since, as Caldon points out, you don't want to modify it at the same time that you're still reading more characters from it.如果您在例如字符串“MyText”上尝试您的程序,那么在第一个循环中,它正确识别“M”是大写字母,因此它在它之前输入一个空格...所以字符串是“MyText”...现在在下一个循环,i=2,并且 s1[i] 又是“M”,因此它在它之前插入一个空格...依此类推...
if you try your program on e.g. string "MyText", then in the first loop it correctly recognizes that "M" is capital, and so it enters one space before it... so the string is " MyText"... now in the next loop, i=2, and s1[i] is again "M" and so it inserts one space before it... and so on...
checks
是该方法的本地变量,并且从未初始化。它可能包含随机数据,但很可能不是您期望的数据(也许它是一个空集)。因此IF
条件可能永远不会成立。checks
is local to the method and never initialized. It may contain random data, but most probably not the data you expect (perhaps it it is an empty set). So theIF
-condition will probably never become true.