For the following problem I came up with the following solution, is it unsound?
Given a string (N 89°56'3" E) add a zero in front of any single digit. i.e. N 89°56'03" E. The string can contain no more than 13 characters and will always start with either "N<space>,S<space>" and end with either "E<space>, W<space>". It will always represent a cardinal direction.
More examples:
change;
N 78°8'4" E --> N 78°08'04" E
N 5°14'9" W --> N 05°14'09" W
;;; FUNCTION
;;; take a bearing as in (N 89°6'33" E) and add a 0 to the minutes(6)
;;; so the output looks like (N 89°06'33" E)
;;;
;;; ARGUMENTS
;;; string in the form of "N 89°56'3\" E"
;;;
;;; USAGE
;;; (fix-bearing "N 89°56'3\" E")
;;; (fix-bearing "N 89°6'33\" E")
;;; (fix-bearing "N 9°26'33\" E")
;;;
;;;
;;; PLATFORMS
;;; 2000+
;;;
;;; AUTHOR
;;; Copyright© 2004 Mark S. Thomas
;;; mark.thomas@theswamp.org
;;;
;;; VERSION
;;; 1.0 Mon Oct 11, 2004 12:43:41
;;; "N 49°26'33\" E" normal string
(defun fix-bearing (str / str_l cntr lst)
(cond
((< (strlen str) 13); a 'normal' string will contain exactly 13 characters
(setq cntr 0 ; make sure counter starts at 0
lst '() ; create a empty list
str_l (vl-string->list str) ; conv. string to list
)
(cond ((= (vl-string-position 176 str) 3)
;; N 9°26'33" E = input string
;; 0123
;; we know that the ° should be in the 4th position
;; if it's not then we need to add a '0' to the degree
;; N 09°26'33" E
(while (< cntr (length str_l))
;; creates a list, first item last
(setq lst (vl-list* (nth cntr str_l) lst))
(if (and
(= (car lst) 32); space
(< (length lst) 3); less than 3 item in the list
)
(setq lst (append '(48) lst))
)
(setq cntr (1+ cntr))
); end of loop
(fix-bearing (vl-list->string (reverse lst)))
); 1st cond.
((= (vl-string-position 39 str) 6)
;; N 89°6'33" E
;; 0123456
;; we know that the ' should be in the 7th position
;; if it's not then we need to add a 0 to the minutes
;; N 89°06'33" E
(while (< cntr (length str_l))
;; creates a list, first item last
(setq lst (vl-list* (nth cntr str_l) lst))
(if (= (car lst) 176); °
(setq lst (append '(48) lst))
)
(setq cntr (1+ cntr))
)
(fix-bearing (vl-list->string (reverse lst)))
); 2nd cond.
((= (vl-string-position 34 str) 9)
;; N 89°16'3" E
;; 0123445678
;; we know that the " should be in the 9th position
;; if it's not then we need to add a 0 to the seconds
;; N 89°16'03" E
(while (< cntr (length str_l))
;; creates a list, first item last
(setq lst (vl-list* (nth cntr str_l) lst))
(if (= (car lst) 39); '
(setq lst (append '(48) lst))
)
(setq cntr (1+ cntr))
)
(fix-bearing (vl-list->string (reverse lst)))
); 3rd cond.
); cond
); 1st cond.
(T str)
); cond
)