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

Source code in the nim programming language

import strformat

proc analyze(str: string) =
    if str.len() > 1:
        var first = str[0]
        for i, c in str:
            if c != first:
                echo "'", str, "': [len: ", str.len(), "] not all characters are the same. starts to differ at index ",
                    i, ": '", first, "' != '", c, "' [", fmt"{ord(c):#x}", "]"
                return
    echo "'", str, "': [len: ", str.len(), "] all characters are the same"

var strings = @["", "   ", "2", "333", ".55", "tttTTT", "4444 444k"]
for str in strings:
    analyze(str)


  

You may also check:How to resolve the algorithm UPC step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Prolog programming language
You may also check:How to resolve the algorithm Iterated digits squaring step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Copy a string step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Arrays step by step in the BBC BASIC programming language