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.
-
With Gnat.IO; use Gnat.IO;- Imports the
Gnat.IOpackage, which provides functions for input and output. use Gnat.IO;allows direct usage of procedures likePut,Put_Line, andGet_Line.
- Imports the
-
procedure gnatiostring is- Defines the procedure
gnatiostring, which acts as the main program.
- Defines the procedure
-
s : String (1 .. 100);- Declares a string variable
swith a fixed size of 100 characters. - The index (1 .. 100) defines a string of length 100.
- Declares a string variable
-
i : Integer;- Declares an integer variable
ito store the actual length of the input string.
- Declares an integer variable
-
begin- Marks the start of the executable code.
-
Put ("Enter a String: ");- Displays a prompt asking the user to enter a string.
-
Get_Line (s, i);- Reads a line of text from the user and stores it in
s. - The variable
istores the actual number of characters entered.
- Reads a line of text from the user and stores it in
-
Put ("Your String is : ");- Displays the message
"Your String is : ".
- Displays the message
-
Put_Line (s (1 .. i));- Prints the actual entered string from index 1 to
i(ignoring unused space).
- Prints the actual entered string from index 1 to
-
end;
- Marks the end of the program.
Key Features:
- Uses
Gnat.IOfor 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