ADA Program to print a string entered by user

Program

With Gnat.IO; use Gnat.IO;
procedure gnatiostring is
	s : String (1 .. 100);
	i : Integer;
begin
	Put ("Enter a String: ");
	Get_Line (s, i);
	Put ("Your String is : ");
	Put_Line (s (1 .. i));
end;

This program showcases basic string handling in Ada. It reads a string input from the user and prints it.

  1. With Gnat.IO; use Gnat.IO;

    • Imports the Gnat.IO package, which provides functions for input and output.
    • use Gnat.IO; allows direct usage of procedures like Put, Put_Line, and Get_Line.
  2. procedure gnatiostring is

    • Defines the procedure gnatiostring, which acts as the main program.
  3. s : String (1 .. 100);

    • Declares a string variable s with a fixed size of 100 characters.
    • The index (1 .. 100) defines a string of length 100.
  4. i : Integer;

    • Declares an integer variable i to store the actual length of the input string.
  5. begin

    • Marks the start of the executable code.
  6. Put ("Enter a String: ");

    • Displays a prompt asking the user to enter a string.
  7. Get_Line (s, i);

    • Reads a line of text from the user and stores it in s.
    • The variable i stores the actual number of characters entered.
  8. Put ("Your String is : ");

    • Displays the message "Your String is : ".
  9. Put_Line (s (1 .. i));

    • Prints the actual entered string from index 1 to i (ignoring unused space).
  10. end;

  • Marks the end of the program.

Key Features:

  • Uses Gnat.IO for input and output.
  • Reads an entire string using Get_Line.
  • Stores actual length to avoid printing extra spaces.
  • Efficiently handles user input in Ada.

Output

$ gnat make gnatiostring.adb
gcc -c gnatiostring.adb
gnatbind -x gnatiostring.ali
gnatlink gnatiostring.ali
$ ./gnatiostring
Enter a String: Welcome
Your String is : Welcome