这段代码对字符串进行非驼峰式大小写有什么问题?

发布于 2024-09-03 17:29:03 字数 1082 浏览 0 评论 0原文

这是我尝试解决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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第七度阳光i 2024-09-10 17:29:03

check 是一个集合,这是正确的,但您尚未为其分配任何值,因此它的值是不确定的。其中没有您期望的字符,因此 in 测试可能总是失败。 (编译器没有警告您尚未分配任何 check 吗?)

您并不真的想将 check 定义为子范围类型。相反,您应该将 check 替换为内置的 TSysCharSet类型,这是一组字符。然后像这样分配 check

check := ['A'..'Z'];

此外,不要使用 intOrd 检查字符串的数值,只需使用 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 the in test probably always fails. (Didn't the compiler warn you that you hadn't assigned anything check yet?)

You don't really want to define check as a subrange type. Rather, you should replace check with the built-in TSysCharSet type, which is a set of characters. Then assign check like this:

check := ['A'..'Z'];

Furthermore, rather than check the numeric value of the string with int and Ord, just use the Char values directly: if s1[i] in check. You'll also want to use the Length 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 than s1 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.

腹黑女流氓 2024-09-10 17:29:03

如果您在例如字符串“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...

寂寞清仓 2024-09-10 17:29:03

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 the IF-condition will probably never become true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文