Problems with string type ... 
Author Message
 Problems with string type ...

Just can't understand why the further programm doesn't work! :-((
Am I right that string is an array of chars where in 0 positition
string's length is keeped?

{-------------------------------}
var
  i:byte;
  str:string;
begin
  for i:=1 to 5 do begin
    str[i]:='a';
  end;
  writeln(str);
end.
{-------------------------------}

Isn't it supposed that str='aaaaa'?! But it isn't! ;-((
Any suggestions?



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

Quote:

> Just can't understand why the further programm doesn't work! :-((
> Am I right that string is an array of chars where in 0 positition
> string's length is keeped?

> {-------------------------------}
> var
>   i:byte;
>   str:string;
> begin
>   for i:=1 to 5 do begin
>     str[i]:='a';
>   end;
>   writeln(str);
> end.
> {-------------------------------}

> Isn't it supposed that str='aaaaa'?! But it isn't! ;-((
> Any suggestions?

1) you forgot the length byte str[0];
   str[0] := #5;  {the # is necessary here...}

2) str is a reserved word, you should not use it as a var name.

Franz Glaser



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

Quote:
>1) you forgot the length byte str[0];
>   str[0] := #5;  {the # is necessary here...}

A-ha!!! ;-))) I knew I forgot something very easy and simple!!! ;-))

So I could put something like: str[0]:=i and this should work .....
To tell the truth I thought it changes automatically with string change.

But why I have to put "#" before number?

Quote:

>2) str is a reserved word, you should not use it as a var name.

Yep, I know. Just was first what came on mind naming string variable.
Next time won't use it.

p.s.: at last my 'long arithmetic' programm-calculator work!!!! ;)))
        it can make things with big numbers [255 cifers - string].

<Jumps around the room, laughes rolling on the floor>



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

Quote:

> >1) you forgot the length byte str[0];
> >   str[0] := #5;  {the # is necessary here...}

> A-ha!!! ;-))) I knew I forgot something very easy and simple!!! ;-))

> So I could put something like: str[0]:=i and this should work .....
> To tell the truth I thought it changes automatically with string change.

> But why I have to put "#" before number?

Because in principle a String is an array[0..255] of CHAR.
The usage of the [0] byte as the string length is somewhat
"illegal", it has a "built in" type conversion in all procs
and funcs, but NOT if you access the [0]th character
explicitly.

Btw: Starting the "text" at [1] has several advantages
over ;-) other language's convention to start the string
at [0]. ;;;---)))

Franz Glaser



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...


Quote:
>Just can't understand why the further programm doesn't work! :-((
>Am I right that string is an array of chars where in 0 positition
>string's length is keeped?

>{-------------------------------}
>var
>  i:byte;
>  str:string;
>begin
>  for i:=1 to 5 do begin
>    str[i]:='a';
>  end;

   str[0] := chr(5);
Quote:
>  writeln(str);
>end.
>{-------------------------------}

>Isn't it supposed that str='aaaaa'?! But it isn't! ;-((
>Any suggestions?

You need to set the length byte as shown above. As the length byte can
be anything in a string that has not had an assignment to it, you could
get any length unless one is explicitly set when assigning characters to
a string that is otherwise unitialised.

Incidentally, I'd avoid Str as a variable - it redefines the Str
procedure and may make some code break that relies on the Str procedure.
--
Pedt

Mobile sig does not exist *yet*



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

Quote:
>>   str[0] := #5;  {the # is necessary here...}
>A-ha!!! ;-)))>
>But why I have to put "#" before number?

Its a shortcut. The real thing is:

str[0]:=chr(5);

Str[0] is of type CHAR and you could only store
characters there.

regards

  Harald



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

Quote:

>2) str is a reserved word, you should not use it as a var name.

Well, actually str is _not_ a reserved word. Pascal has a very small set of
reserved words.

Using str as a variable name is legal, but, yes, should be avoided. If you
do, you might have to refer to the str procedure as system.str...

Then again, I have used something like this now and then:

Function Str(N:Word):String;
Var S:String;
Begin
  System.Str(N,S);
  Str:=S;
End;

--
/GreenGhost
________________________
Kill the spammer to mail me



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...
I understand the problem with "str" variable; it's a procedure name
so that's why I couldn't use it. I just was typing fast my example and
typed in the first word which associated in my mind with "string" ;))
so I typed "str" as variable. I understand that I shouldn't do such mistakes
even in tiny useless programs but I think I did it coz of lack of experience
;(

Thanks for pointing this out, I will try to avoid this problem in future.

Quote:


>>2) str is a reserved word, you should not use it as a var name.

>Well, actually str is _not_ a reserved word. Pascal has a very small set of
>reserved words.

>Using str as a variable name is legal, but, yes, should be avoided. If you
>do, you might have to refer to the str procedure as system.str...

>Then again, I have used something like this now and then:

>Function Str(N:Word):String;
>Var S:String;
>Begin
>  System.Str(N,S);
>  Str:=S;
>End;

>--
>/GreenGhost
>________________________
>Kill the spammer to mail me



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...
  I have always used the following format, to change the length index in
a string.
str[0] := chr(5);
or
str[0] := char(5);
or
str[0] := chr(newstrlg);
or
str[0] := char(newstrlg);
etc..

  The only advantage to doing it this way, is if you don't know what the
length of the string is going to be in advance.  Correct me if I'm wrong
here, but you can't precede a variable name with the # sign can you?

Bill Mott

str[0] := chr(5);

Quote:


> > Just can't understand why the further programm doesn't work! :-((
> > Am I right that string is an array of chars where in 0 positition
> > string's length is keeped?

> > {-------------------------------}
> > var
> >   i:byte;
> >   str:string;
> > begin
> >   for i:=1 to 5 do begin
> >     str[i]:='a';
> >   end;
> >   writeln(str);
> > end.
> > {-------------------------------}

> > Isn't it supposed that str='aaaaa'?! But it isn't! ;-((
> > Any suggestions?

> 1) you forgot the length byte str[0];
>    str[0] := #5;  {the # is necessary here...}

> 2) str is a reserved word, you should not use it as a var name.

> Franz Glaser



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...

us with
Quote:
>  Correct me if I'm wrong
>here, but you can't precede a variable name with the # sign can you?

If you are looking to compare a char variable with the ascii code then
it is perfectly acceptable to use #

temp := #32
and
temp := chr(32);

are identical if temp is a char.

--
Pedt



Wed, 18 Jun 1902 08:00:00 GMT  
 Problems with string type ...
Hi Bill

Quote:
>  I have always used the following format, to change the length index in
>a string.
>str[0] := chr(5);

That's how i also do. You can use
str[0]:=#5, but only for a constant value.

But what will happen if you will
set str[0] to a higher value than the
string can hold, i.e.:

var str:string[10]
str[0]:=#20; (never tried this...)

Wouldn't it be saver to use
str:=copy(str,1,n) instead?

JOE :-))



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. String type and optimization

2. Type String

3. Pos Type string search for dBase field

4. Problem with BYTE data type

5. type library Problem

6. MSACCESS: Problems with AutoInc-Field, Type: Replication-ID

7. Some Problems With Field Types, etc.

8. Problems of ordinary types

9. BP type REAL problem

10. FPC & BP- Set type problems

11. Interbase[5] - get all descendents/ancestors (Parts List type problem)

12. Q: Oracle or BDE data type problem???

 

 
Powered by phpBB® Forum Software