Penny Allison
Penny Allison

Reputation: 27

How to get a smaller string from an unbounded string

I am having trouble reading in a line from a file and then breaking it up into its individual words. Let's say that I read in "When the night was young" because that was the first line, I cannot figure out how to just get the word "When" away from the rest of it, I've tried multiple times, and I've run out of ideas. I'm fairly new to unbounded strings in Ada and just Ada in general. Any help is appreciated, little hints or the solution to my problem, thanks.

with Ada.Text_IO;                   use Ada.Text_IO;
with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;

with AVL; use AVL;

procedure Spellchecker is
   InWord      : Unbounded_String;
   ReadIn      : String (1..20);
   Break       : Character := ' ';
   RevisedWord : String(1..20);
   Dictionary  : File_Type;
   Paragraph   : File_Type;
   Count       : Integer;
   LastChar    : Integer;
   NewTree     : Tree;

   type Spell is array (Integer range 1..20) of Character;
   Revision : Spell;

begin
   Ada.Text_IO.Open (File => Dictionary,
                     Mode => In_File,
                     Name => "dictionary.txt");
   loop
      exit when End_of_File (File => Dictionary);
      InWord := Get_Line (File => Dictionary);
      Insert (InWord, NewTree);
   end loop;
   Close (File => Dictionary);
   Print (NewTree);
   InWord := Get_Line (File => Paragraph);
   Count := 1;
   Put (InWord);
end Spellchecker;

Upvotes: 2

Views: 1089

Answers (1)

Jacob Sparre Andersen
Jacob Sparre Andersen

Reputation: 6611

There are at least two options:

  • You can convert the Unbounded_String to a regular fixed-length String and operate on that.
  • You can look up the subprograms Index and Slice in the section of the reference manual describing Ada.Strings.Unbounded, and use those to find spaces and cut the Unbounded_String appropriately.

Upvotes: 2

Related Questions