Skip to content

모듈12 String문자열들 Exercise 3 이슈 #2

Description

@pioneerprizehun
unit Unit_ch12_3;

interface

uses
  System.Classes, Vcl.Controls, Winapi.Messages, System.SysUtils, System.Variants, Vcl.Graphics,
  Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,System.StrUtils;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
  Cnt:integer;
  s:string;
begin
  Cnt := 0;
  s := Edit1.Text;
  for i := 1 to Length(s) do
    if s[i] =  '.' then
       Cnt := Cnt + 1;
   ShowMessage(InTtoStr(Cnt));
end;
end.

위코드는 Exercise 3 의 코드입니다.

예제프로젝트를 실행시 작동이 잘되지만
직접 작성하여 실행해보면 되지않습니다.

문제는

밑에서부터 위로6번째줄

  for i := 0 to Length(s) do```
 에있습니다.

문자열의 첫번째는 길이가 들어 있고, 이곳은 직접 읽고 쓸수 없습니다.

그래서 첫번째를 건너 띄고, 1 인덱스부터 for 문을 돌려야 합니다. 

아래의 코드로 작성시 작동되오니 혹시 오류가생겨 막히는분들에게 참고가되는 글이였으면좋겠습니다.

for i := 1 to Length(s) do

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions