How to resolve the algorithm Odd word problem step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Odd word problem step by step in the Prolog programming language

Table of Contents

Problem Statement

Write a program that solves the odd word problem with the restrictions given below.

You are promised an input stream consisting of English letters and punctuations.
It is guaranteed that:

A stream with six words:

The task is to reverse the letters in every other word while leaving punctuations intact, producing: while observing the following restrictions:

Work on both the   "life"   example given above, and also the text:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Odd word problem step by step in the Prolog programming language

Source code in the prolog programming language

odd_word_problem :-
	read_line_to_codes(user_input, L),
	even_word(L, Out, []),
	string_to_list(Str, Out),
	writeln(Str).

even_word(".") --> ".".

even_word([H | T]) -->
	{char_type(H,alnum)},
	[H],
	even_word(T).

even_word([H | T]) -->
	[H],
	odd_word(T, []).

odd_word(".", R) --> R, ".".

odd_word([H|T], R) -->
	{char_type(H,alnum)},
	odd_word(T, [H | R]).

odd_word([H|T], R) -->
	R,
	[H],
	even_word(T).


  

You may also check:How to resolve the algorithm FTP step by step in the Raku programming language
You may also check:How to resolve the algorithm String concatenation step by step in the zkl programming language
You may also check:How to resolve the algorithm Permutations step by step in the Action! programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Joy programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Trith programming language