comp.lang.ada
 help / color / mirror / Atom feed
* Anyway to change the length of a string ?
@ 2002-08-14 19:56 Jim
  2002-08-14 20:12 ` David C. Hoos
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jim @ 2002-08-14 19:56 UTC (permalink / raw)


.... 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"

because 's' has to be 10 characters long right ?

can i have it so i can change the length of 's' from 10 char to 5
characters in the program and then input the value as "hello" ?

thanks



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Anyway to change the length of a string ?
  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
  2 siblings, 0 replies; 5+ messages in thread
From: David C. Hoos @ 2002-08-14 20:12 UTC (permalink / raw)


Use the package Ada.Strings.Unbounded or
Ada.Strings.Bounded instead of using fixed-length strings.

----- Original Message ----- 
From: "Jim" <genx54321@hotmail.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, August 14, 2002 2:56 PM
Subject: Anyway to change the length of a string ?


> .... 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"
> 
> because 's' has to be 10 characters long right ?
> 
> can i have it so i can change the length of 's' from 10 char to 5
> characters in the program and then input the value as "hello" ?
> 
> thanks
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Anyway to change the length of a string ?
  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
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2002-08-14 20:39 UTC (permalink / raw)


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" & "     ";

> because 's' has to be 10 characters long right ?
> 
> can i have it so i can change the length of 's' from 10 char to 5
> characters in the program and then input the value as "hello" ?

See the standard package Ada.Strings.Unbounded.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Anyway to change the length of a string ?
@ 2002-08-14 21:31 Warren W. Gay VE3WWG
  0 siblings, 0 replies; 5+ messages in thread
From: Warren W. Gay VE3WWG @ 2002-08-14 21:31 UTC (permalink / 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





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Anyway to change the length of a string ?
  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
  2 siblings, 0 replies; 5+ messages in thread
From: tmoran @ 2002-08-14 21:45 UTC (permalink / raw)


> now i declare a string which would be used in the program for the word
> which can be 1-10 letters.
"An object of a particular Bounded_String type represents a String whose
low bound is 1 and whose length can vary conceptually between 0 and a
maximum size established at the generic instantiation."  ARM A.4.4(1)
Sound like what you want?



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-08-14 21:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2002-08-14 21:31 Warren W. Gay VE3WWG

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