基本上,在这段代码中,我创建了一个包含5个元素的字符串数组。然后通过使用循环,我应该打印出所有以"s"结尾的单词,甚至它们的行号(1到5(。我应该如何为这段代码编写for循环(或任何其他类型的循环/递归(?
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
Procedure Line_Words is
type data is array (1..5) of Unbounded_String;
A: data;
begin
A(1):= To_Unbounded_String("Peas");
A(2):= To_Unbounded_String("Bone");
A(3):= To_Unbounded_String("As");
A(4):= To_Unbounded_String("Sos");
A(5):= To_Unbounded_String("Found");
for I in data'Range loop
?????????????????? -- A loop that prints out all the words ending with 's' and their line-numbers.
end Line_words ;
查看包Ada.Strings.Unbounded。它包含一个名为Tail的函数,该函数返回一个无界字符串。
function Tail (Source : in Unbounded_String;
Count : in Natural;
Pad : in Character := Space)
return Unbounded_String;
如果将无界字符串作为Source参数传递,并将1作为Count参数传递,则函数将返回一个仅包含Source无界字符串中最后一个字符的无界字符串。
不能为无边界字符串数组打印行号。您可以打印出每个以"s"结尾的无界字符串的数组索引号。想想如何将Tail的返回值与从To_Unboundd_string("s"(创建的无界字符串进行比较。