How to resolve the algorithm Palindrome detection step by step in the SparForte programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindrome detection step by step in the SparForte programming language

Table of Contents

Problem Statement

A palindrome is a phrase which reads the same backward and forward. Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes) is a palindrome. For extra credit:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindrome detection step by step in the SparForte programming language

Source code in the sparforte programming language

#!/usr/local/bin/spar
pragma annotate( summary, "palindrome" );
pragma annotate( description, "Write at least one function/method (or whatever it is" );
pragma annotate( description, "called in your preferred language) to check if a" );
pragma annotate( description, "sequence of characters (or bytes) is a palindrome or" );
pragma annotate( description, "not. The function must return a boolean value (or" );
pragma annotate( description, "something that can be used as boolean value, like an" );
pragma annotate( description, "integer)." );
pragma annotate( see_also, "http://rosettacode.org/wiki/Palindrome_detection" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );

pragma restriction( no_external_commands );

procedure palindrome is

  function is_palindrome( text : string ) return boolean is
  begin
    for offset in 0..strings.length( text ) / 2 -1 loop
      if strings.element( text, offset+1) /= strings.element( text, positive( strings.length( text ) - offset ) ) then
         return false;
      end if;
    end loop;
    return true;
  end is_palindrome;

  sentence : string;
  result   : boolean;
begin
  sentence := "this is a test";
  result   := is_palindrome( sentence );
  put(  sentence ) @ ( " : " ) @ ( result );
  new_line;

  sentence := "ablewasiereisawelba";
  result   := is_palindrome( sentence );
  put(  sentence ) @ ( " : " ) @ ( result );
  new_line;
end palindrome;

  

You may also check:How to resolve the algorithm Problem of Apollonius step by step in the Wren programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Beef programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Scala programming language
You may also check:How to resolve the algorithm N'th step by step in the RPL programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the Phix programming language