How to resolve the algorithm Determine if a string has all the same characters step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Determine if a string has all the same characters step by step in the Prolog programming language
Table of Contents
Problem Statement
Given a character string (which may be empty, or have a length of zero characters):
Use (at least) these seven test values (strings):
Show all output here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string has all the same characters step by step in the Prolog programming language
Source code in the prolog programming language
:- system:set_prolog_flag(double_quotes,chars) .
main
:-
same_or_different("") ,
same_or_different(" ") ,
same_or_different("2") ,
same_or_different("333") ,
same_or_different(".55") ,
same_or_different("tttTTT") ,
same_or_different("4444 444k")
.
%! same_or_different(INPUTz0)
same_or_different(INPUTz0)
:-
system:format('input string is "~s" .~n',[INPUTz0]) ,
examine(INPUTz0)
.
%! examine(INPUTz0)
examine([])
:-
! ,
system:format('all the same characters .~n',[])
.
examine([COMPARE0|INPUTz0])
:-
examine(INPUTz0,COMPARE0,2,_INDEX_)
.
%! examine(INPUTz0,COMPARE0,INDEX0,INDEX)
examine([],_COMPARE0_,INDEX0,INDEX0)
:-
! ,
system:format('all the same characters .~n',[])
.
examine([COMPARE0|INPUTz0],COMPARE0,INDEX0,INDEX)
:-
! ,
INDEX1 is INDEX0 + 1 ,
examine(INPUTz0,COMPARE0,INDEX1,INDEX)
.
examine([DIFFERENT0|_INPUTz0_],COMPARE0,INDEX0,INDEX0)
:-
prolog:char_code(DIFFERENT0,DIFFERENT_CODE) ,
system:format('character "~s" (hex ~16r) different than "~s" at 1-based index ~10r .~n',[[DIFFERENT0],DIFFERENT_CODE,[COMPARE0],INDEX0])
.
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Ring programming language
You may also check:How to resolve the algorithm Gotchas step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Filter step by step in the Racket programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Icon and Unicon programming language