How to resolve the algorithm URL encoding step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm URL encoding step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Provide a function or mechanism to convert a provided string into URL encoding representation. In URL encoding, special characters, control characters and extended characters are converted into a percent symbol followed by a two digit hexadecimal code, So a space character encodes into %20 within the string. For the purposes of this task, every character except 0-9, A-Z and a-z requires conversion, so the following characters all require conversion by default:

The string "http://foo bar/" would be encoded as "http%3A%2F%2Ffoo%20bar%2F".

It is permissible to use an exception string (containing a set of symbols that do not need to be converted). However, this is an optional feature and is not a requirement of this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm URL encoding step by step in the Common Lisp programming language

Source code in the common programming language

(defun needs-encoding-p (char)
  (not (digit-char-p char 36)))

(defun encode-char (char)
  (format nil "%~2,'0X" (char-code char)))

(defun url-encode (url)
  (apply #'concatenate 'string
         (map 'list (lambda (char)
                      (if (needs-encoding-p char)
                          (encode-char char)
                          (string char)))
              url)))

(url-encode "http://foo bar/")


  

You may also check:How to resolve the algorithm Caesar cipher step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Scala programming language
You may also check:How to resolve the algorithm Yahoo! search interface step by step in the Gambas programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the EasyLang programming language