comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca>
Subject: Re: Anyway to change the length of a string ?
Date: Wed, 14 Aug 2002 17:31:01 -0400
Date: 2002-08-14T17:31:01-04:00	[thread overview]
Message-ID: <3D5ACC15.2050701@cogeco.ca> (raw)

Stephen Leake wrote:
 > genx54321@hotmail.com (Jim) writes:
 >>.... after you have already declared it before BEGIN ?
 >>
 >>i am making a Hangman game with words up to 10 letters long.
 >>
 >>now i declare a string which would be used in the program for the word
 >>which can be 1-10 letters.
 >>
 >>so i say
 >>
 >>s : string(1..10)
 >>
 >>but in the program i cant say
 >>
 >>s:= "hello"
 >
 > You can say
 >
 > S (1 .. 5) := "hello";
 >
 > or
 >
 > S := "hello" & "     ";

These are all reasonable solutions.

Expanding upon the above solutions slightly, examine the
following tested piece of code for an example:

with Ada.Text_IO;

procedure EG is
     use Ada.Text_IO;

     function Length(S : String) return Natural is
     begin
        return S'Length;
     end Length;

     S :      String(1..10) := "1234567890";
     Last :   Natural := S'Last;
begin
     Put_Line("S='" & S(1..Last) & "'");

-- Last       := 3; -- or..
     Last       := Length("Cat");  -- Because can't use "Cat"'Last
     S(1..Last) := "Cat";

     Put_Line("S='" & S(1..Last) & "'");

end EG;

The idea is that you work with a variable called Last
(or something like it). If you don't like counting
characters in a larger constant like "Some message..."
then the use of a Lenght() function can be helpful (see
the statement Last := Length("cat");). Unfortunately,
doing "Some very long string constant"'Last is not legal.

At other times, you declare the variable when you
know its length, like:

declare
     My_New_String : String(1..Computed_Length);
begin
     ...

Or you have a function return the exact length string
you need, as in:

declare
     Returned_String : String := My_Function(whatever);
begin
     ...

and then thow it away when you're done with it using:

end;

of the declare..begin..end block. You can do this for
each iteration within a loop as well.

This requires a little different planning than C programmers
are used to. But once you catch onto the general paradigm
shift, you'll find that Ada fixed strings, packages
Ada.Strings.Fixed and Ada.Characters.Handling cover most
of your string needs.

If you deal with a number of variable length strings, then
sometimes resorting to Ada.Strings.Unbounded makes your
life easier as many have already pointed out. However, I
find that once you cross over to Ada.Strings.Unbounded, then
other features of these strings become "less natural" (for
example you must use a Length function instead of a Length
attribute).

Hope this helps, Warren.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg





             reply	other threads:[~2002-08-14 21:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-14 21:31 Warren W. Gay VE3WWG [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-08-14 19:56 Anyway to change the length of a string ? Jim
2002-08-14 20:12 ` David C. Hoos
2002-08-14 20:39 ` Stephen Leake
2002-08-14 21:45 ` tmoran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox