comp.lang.ada
 help / color / mirror / Atom feed
* string operations
@ 2000-11-18  0:00 Margit Gut
  2000-11-18  0:00 ` Jeff Carter
  2000-11-19  0:00 ` Robert Dewar
  0 siblings, 2 replies; 9+ messages in thread
From: Margit Gut @ 2000-11-18  0:00 UTC (permalink / raw)


Hi,

my name is Margit and I am new here. I just start to learn ADA and I
have some problems in understandig what I am doing.

I try to read a string from the keyboard and then I want to write a
recursiv function to turn the characters  from the end to the beginning.

I fetch the string with Get_Line. I declared the string with
Sstring : string(1..255);
So whatever I try, the string'Length is always 255.
I can't turn this string to the opposite unless it has its correct
Length (5 for hello)
How must I declare a string, so that I can eliminate each character
separate and its Length is the correct one?
 My next poblem is, how to do a correct Function, that calls itselve.
I tried several possibilitoes, but I think I didn't understand the
problem correct.
Last question: How can I initiate a string with ""?

Can someone help me please?

thanks

Margit





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

* Re: string operations
  2000-11-18  0:00 string operations Margit Gut
@ 2000-11-18  0:00 ` Jeff Carter
  2000-11-19  0:00 ` Robert Dewar
  1 sibling, 0 replies; 9+ messages in thread
From: Jeff Carter @ 2000-11-18  0:00 UTC (permalink / raw)


Margit Gut wrote:
> I try to read a string from the keyboard and then I want to write a
> recursiv function to turn the characters  from the end to the beginning.

The requirement that this be a recursive function sounds like a homework
problem.

> 
> I fetch the string with Get_Line. I declared the string with
> Sstring : string(1..255);
> So whatever I try, the string'Length is always 255.

Given the declaration

S : String (1 .. 255);

S'Length is always 255. This is the definition of the 'Length attribute
on an array object.

Look carefully at the description of Get_Line in the ARM (A.10.7),
especially the meaning of the Last parameter, and then read about slices
(ARM 4.1.2). You can find the ARM on line at

http://www.adapower.com/rm95/index.html

If the string read by Get_Line is shorter than its Item parameter, it is
stored in Item (Item'First .. Last). Thus, after executing

Get_Line (Item => S, Last => Last);

the string of interest is in the slice

S (S'First .. Last)

>  My next poblem is, how to do a correct Function, that calls itselve.

A function calls itself exactly the same way that any other statement
calls the function:

function F (N : Positive) return Positive is
begin -- F
   if N <= 1 then
      return N;
   else
      return N * F (N - 1);
   end if;
end F;

-- 
Jeff Carter
"You tiny-brained wipers of other people's bottoms!"
Monty Python & the Holy Grail





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

* Re: string operations
  2000-11-18  0:00 string operations Margit Gut
  2000-11-18  0:00 ` Jeff Carter
@ 2000-11-19  0:00 ` Robert Dewar
  2000-11-20  0:00   ` Anders Wirzenius
  1 sibling, 1 reply; 9+ messages in thread
From: Robert Dewar @ 2000-11-19  0:00 UTC (permalink / raw)


In article <3A16A4D5.CE941970@t-online.de>,
  Margit Gut <Margit.Gut@t-online.de> wrote:
> Hi,
>
> my name is Margit and I am new here. I just start to learn ADA
and I
> have some problems in understandig what I am doing.
>
> I try to read a string from the keyboard and then I want to
write a
> recursiv function to turn the characters  from the end to the
beginning.,


This assignment has nothing to do with Ada specifically. The
requirement for a recursive solution is the DOMINANT component
of the assignment. You need to formulate an appropriate
recursive approach completely independent of the language in
which you will write, and you have not got that far, so your
attempts to turn your thinking into Ada are very premature.

Here are some hints:

1. Your solution must be able to work for arbitrary length
input, so any constant like 255 is immediately wrong.

2. You need to think in terms of reading individual characters

Learning to formulate recursive solutions to problems is a
very challenging problem for many students, and I would say
that many professional programmers have never learned this
approach. One valuable excercise I have found is to write
a reasonably complex program with no assignments at all.

Here is my favorite example of recursion, to output an integer
without worrying about its size:

  with Text_IO; use Text_IO;
  procedure Print_Nat (N : Natural) is
  begin
     if N >= 10 then
        Print_Nat (N / 10);
     end if;

     Put (Character'Val (N mod 10 + Character'Pos ('0')));
  end Print_Nat;

I like this example since it is not simple tail recursion like
the factorial example, and it is in fact very hard work to turn
this into a loop without imposing some arbitrary limit on the
length. Note that this algorithm would work fine in a language
that had arbitrary precision integers.






Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: string operations
  2000-11-19  0:00 ` Robert Dewar
@ 2000-11-20  0:00   ` Anders Wirzenius
  2000-11-20  0:00     ` Robert Dewar
  0 siblings, 1 reply; 9+ messages in thread
From: Anders Wirzenius @ 2000-11-20  0:00 UTC (permalink / raw)


"Robert Dewar" <robert_dewar@my-deja.com> wrote in message
news:8v90vm$j5f$1@nnrp1.deja.com...
>
> ...
>
> Here is my favorite example of recursion, to output an integer
> without worrying about its size:
>
> ...
>
>      Put (Character'Val (N mod 10 + Character'Pos ('0')));
>   end Print_Nat;
>
> I like this example since it is not simple tail recursion like

I am a person studying Ada at my spare time (and, sorry, using comp.lang.ada
very much as a learning source).
I like your example, too. It shows both recursion and string handling,
thanks for that. To me the example would have been even more revealing if
the Put statement had been written
Put(Character'Val(Character'Pos ('0') + N mod 10));

Anders Wirzenius






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

* Re: string operations
  2000-11-20  0:00   ` Anders Wirzenius
@ 2000-11-20  0:00     ` Robert Dewar
  2000-11-21  6:30       ` Anders Wirzenius
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Dewar @ 2000-11-20  0:00 UTC (permalink / raw)


In article <WE7S5.239$rj6.8595@read2.inet.fi>,
  "Anders Wirzenius" <anders.wirzenius@pp.qnet.fi> wrote:
> To me the example would have been even more revealing if
> the Put statement had been written
> Put(Character'Val(Character'Pos ('0') + N mod 10));


Odd ... addition is after all commutative, can you explain why
in this case a+b is clearer than b+a

I personally prefer the form I had, because from an algorithmic
point of view it is N mod 10 that is important, and
the Character'Pos is a minor detail for keeping the types
right ...



Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: string operations
  2000-11-20  0:00     ` Robert Dewar
@ 2000-11-21  6:30       ` Anders Wirzenius
  2000-11-24  0:00         ` Margit Gut
  2000-11-24  0:00         ` Margit Gut
  0 siblings, 2 replies; 9+ messages in thread
From: Anders Wirzenius @ 2000-11-21  6:30 UTC (permalink / raw)



"Robert Dewar" <robert_dewar@my-deja.com> wrote in message
news:8vbfns$do2$1@nnrp1.deja.com...
> In article <WE7S5.239$rj6.8595@read2.inet.fi>,
>   "Anders Wirzenius" <anders.wirzenius@pp.qnet.fi> wrote:
> > To me the example would have been even more revealing if
> > the Put statement had been written
> > Put(Character'Val(Character'Pos ('0') + N mod 10));
>
>
> Odd ... addition is after all commutative, can you explain why
> in this case a+b is clearer than b+a
>
> I personally prefer the form I had, because from an algorithmic
> point of view it is N mod 10 that is important, and
> the Character'Pos is a minor detail for keeping the types
> right ...

Ok. As i wrote, the example deals with both the algoritm of recursion and
the string handling.
I just emphasized the string handling more than as "a minor detail".  :-)

By the way. I hope miss Margit played a little with the example, as I did,
and switched the order of the statements in the procedure.

Anders





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

* String operations
@ 2000-11-24  0:00 Margit Gut
  0 siblings, 0 replies; 9+ messages in thread
From: Margit Gut @ 2000-11-24  0:00 UTC (permalink / raw)


Hi,

I needed some days, but now it runs. Many thanks to everyone

Margit

mailto: Margit.Gut@t-online.de






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

* Re: string operations
  2000-11-21  6:30       ` Anders Wirzenius
  2000-11-24  0:00         ` Margit Gut
@ 2000-11-24  0:00         ` Margit Gut
  1 sibling, 0 replies; 9+ messages in thread
From: Margit Gut @ 2000-11-24  0:00 UTC (permalink / raw)
  To: Anders Wirzenius

Hi,

I needed some time but now it runs. Many thanks to everyone

Margit

mailto: Margit.Gut@t-online.de







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

* Re: string operations
  2000-11-21  6:30       ` Anders Wirzenius
@ 2000-11-24  0:00         ` Margit Gut
  2000-11-24  0:00         ` Margit Gut
  1 sibling, 0 replies; 9+ messages in thread
From: Margit Gut @ 2000-11-24  0:00 UTC (permalink / raw)
  To: Anders Wirzenius

Hi,

I needed some time but now it runs. Many thanks to everyone

Margit

mailto: Margit.Gut@t-online.de







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

end of thread, other threads:[~2000-11-24  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-24  0:00 String operations Margit Gut
  -- strict thread matches above, loose matches on Subject: below --
2000-11-18  0:00 string operations Margit Gut
2000-11-18  0:00 ` Jeff Carter
2000-11-19  0:00 ` Robert Dewar
2000-11-20  0:00   ` Anders Wirzenius
2000-11-20  0:00     ` Robert Dewar
2000-11-21  6:30       ` Anders Wirzenius
2000-11-24  0:00         ` Margit Gut
2000-11-24  0:00         ` Margit Gut

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