How to resolve the algorithm String case step by step in the Metafont programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm String case step by step in the Metafont programming language

Table of Contents

Problem Statement

Take the string     alphaBETA     and demonstrate how to convert it to:

Use the default encoding of a string literal or plain ASCII if there is no string literal in your language. Note: In some languages alphabets toLower and toUpper is not reversable. Show any additional case conversion functions   (e.g. swapping case, capitalizing the first letter, etc.)   that may be included in the library of your language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm String case step by step in the Metafont programming language

Source code in the metafont programming language

vardef isbetween(expr a, i, f) =
  if string a:
    if (ASCII(a) >= ASCII(i)) and (ASCII(a) <= ASCII(f)):
      true
    else:
      false
    fi
  else:
    false
  fi enddef;

vardef toupper(expr s) =
  save ?; string ?; ? := ""; d := ASCII"A" - ASCII"a";
  for i = 0 upto length(s)-1:
    if isbetween(substring(i, i+1) of s, "a", "z"):
      ? := ? & char(ASCII(substring(i,i+1) of s) + d)
    else:
      ? := ? & substring(i, i+1) of s
    fi;
  endfor
  ?
enddef;

vardef tolower(expr s) =
  save ?; string ?; ? := ""; d := ASCII"a" - ASCII"A";
  for i = 0 upto length(s)-1:
    if isbetween(substring(i, i+1) of s, "A", "Z"):
      ? := ? & char(ASCII(substring(i,i+1) of s) + d)
    else:
      ? := ? & substring(i, i+1) of s
    fi;
  endfor
  ?
enddef;

message toupper("alphaBETA");
message tolower("alphaBETA");

end

  

You may also check:How to resolve the algorithm Draw a sphere step by step in the Racket programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the VBScript programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the PHP programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the jq programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Kotlin programming language