* Novice String Question
@ 1999-09-01 0:00 Ray
0 siblings, 0 replies; only message in thread
From: Ray @ 1999-09-01 0:00 UTC (permalink / raw)
I am working on learning strings, so forgive the creaky code.
I can get the �Replace_Slice� to work if Y and Z are the same length,
otherwise the last Put_Line will raise an exception.
1. Do I need, and how can I get the Drop and Justify parameters to work?
2. Should I be using something else?
3. How can I convert this to either bounded or unbounded (have not been
able to get the declares/withs right)?
-- code
with Ada.Text_Io, Ada.integer_text_io, Ada.Strings.Fixed;
use Ada.Text_Io, Ada.integer_text_io, Ada.Strings.Fixed;
--
-- Cohen 'Ada as a 2nd language' 2nd ed. Page 225.
-- Exercise 5.2, read a source X, pattern Y, and replace Z strings.
-- Replace the first occurrence of the pattern.
-- Assume max string length is 80.
--
procedure EX5_2 is
X, Y, Z: string (1 .. 80);
I_X, I_Y, I_Z : Natural;
begin
get_line (X, I_X); -- get the strings
get_line (Y, I_Y);
get_line (Z, I_Z);
-- find start of first match
Flag_InStr := Index(X(X'first .. I_X), Y (Y'first .. I_Y));
If Flag_InStr /= 0 then -- do we have a match?
X := Replace_Slice
(X,
Flag_InStr,
Flag_InStr + I_Y - 1,
Z(Z'first .. I_Z));
I_X := I_X + (I_Z - I_Y); -- correct X end index for Z and Y not =
length
end if;
put_line (X (X'first .. I_X)); -- echo the modified string
end EX5_2;
--
Remove the numbers from my address to reply.
Ray
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1999-09-01 0:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-01 0:00 Novice String Question Ray
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox