Javatpoint Logo
Javatpoint Logo

Output and Input of Characters in Prolog

Outputting Characters

We can use the built-in predicate put/1 to provide the output of characters. This predicate takes a single argument, and that argument is a number from 0 to 255 or an expression. When we evaluate put goal, due to this, a single character will output to the current output stream. This character corresponds to a numerical value and produces the ASCII value of its argument.

For example:

Inputting Characters

We can use two built-in predicates get/1 and get0/1 to provide the input of a single character. The predicate get0 takes a single argument as a variable. When we evaluate get0 goal, due to this, a character will be read from the current input stream.

Note that if the user input is required from the keyboard, a prompt character like colon will be displayed. Before Prolog accepts the input from the user, we have to press the 'return' key.

When Prolog evaluates a get0 goal, the variable is unified with the ASCII value of the input character. If the variable of the argument is unbound, it is bound to the ASCII value.

For example:

If the variable of the argument is already bound, the goal succeeds if and only if the ASCII value of input character and numerical value is the same.

For example:

The get predicate takes a single argument as a variable. When we evaluate get goal, due to this, the next non-white-space character will be read from the current input stream. A non-white-space character means a character with an ASCII value less than or equal to 32. Same as get0 predicate, the variable and ASCII value of the character unifies.

For example

Examples of Using Characters

In the first example, we will show how to read the character in a series from the keyboard. It finishes with * and outputs their corresponding ASCII values one per line.

The readin predicate is defined recursively. When we evaluate this predicate, due to this, we will input a single character, and variable A will be bound to its ASCII value. In the process(X) goal, the action depends on whether A contains the value 42 or not, which signifies a * character. If not, A's value is output and followed by a new line and again call to readlin predicate. If it has, the evaluation of process(X) goal stops. When Prolog reads the * character, the process will stop. In the following example, 69, 120, 97, 109, etc. shows the ASCII value of characters E, x, a, m, etc.


In the second example, a number of characters are output instead of the ASCII value of input character. For this, we will use the count predicate. This predicate takes two arguments. The first argument read as 'the number of characters counted so far', and the second argument read as 'the total number of characters before *'.



Next TopicProlog





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA