How to resolve the algorithm Determine if a string has all the same characters step by step in the Erlang 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 Erlang 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 Erlang programming language

Source code in the erlang programming language

-module(string_examples).
-export([examine_all_same/1, all_same_examples/0]).

all_same_characters([], _Offset) ->
    all_same;
all_same_characters([_], _Offset) ->
    all_same;
all_same_characters([X, X | Rest], Offset) ->
    all_same_characters([X | Rest], Offset + 1);
all_same_characters([X, Y | _Rest], Offset) when X =/= Y ->
    {not_all_same, Y, Offset + 1}.

examine_all_same(String) ->
    io:format("String \"~ts\" of length ~p:~n", [String, length(String)]),
    case all_same_characters(String, 0) of
        all_same ->
            io:format("  All characters are the same.~n~n");
        {not_all_same, OffendingChar, Offset} ->
            io:format("  Not all characters are the same.~n"),
            io:format("  Char '~tc' (0x~.16b) at offset ~p differs.~n~n",
                      [OffendingChar, OffendingChar, Offset])
    end.

all_same_examples() ->
    Strings = ["",
               "   ",
               "2",
               "333",
               ".55",
               "tttTTT",
               "4444 444k",
               "pépé",
               "🐶🐶🐺🐶",
               "🎄🎄🎄🎄"],
    lists:foreach(fun examine_all_same/1, Strings).


  

You may also check:How to resolve the algorithm Iterated digits squaring step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the XLISP programming language
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the J programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Nim programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Maxima programming language