Text to Morse and Back Again PDF Print E-mail
User Rating: / 0
PoorBest 
Written by regicide666   
Tuesday, 15 August 2006

#!/bin/bash
# amcoder written for binrev forums programming challenge #5
# by regicide666
# converts text to morse code and morse code to text

#http://www.binrev.com/forums/index.php?showtopic=22332&st=0&gopid=202112&#entry202112
morse=(.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. \
        -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- \
        --.. .---- ..--- ...-- ....- ..... -.... --... ---.. \
        ----. ----- " ")

alfanum=(a b c d e f g h i j k l m n o p q r s t u v w x y z \
            1 2 3 4 5 6 7 8 9 0 " ")
            

function echoall {
    count=0
    while (( $count <= 36 ))
    do
        echo -n "${alfanum[$count]} = ${morse[$count]} "
        ((count++))
    done
}

function astoms {
    count=0
    while (( $count < ${#string} ))
    do
        count1=0
        until [[ ${string:$count:1} == ${alfanum[$count1]} || $count1 == "37" ]]
        do
            ((count1++))
        done
        if [[ $count1 == 37 ]]
        then
            echo " Unknown Charachter ${string:$count:1} "
            exit 1
        else
            output[$count]=${morse[$count1]}
        fi
    ((count++))
    done
}

function mstoas {
    count=0
    while (( $count < ${#codes[*]} ))
    do
        count1=0
        until [[ ${codes[$count]} == ${morse[$count1]} || $count1 == "37" ]]
        do
            ((count1++))
        done
        if [[ $count1 == 37 ]]
        then
            echo " Unknown Code ${codes[$count]} "
            exit 1
        else
        output[$count]=${alfanum[$count1]}
        ((count++))
        fi
    done
}

function usage {
    echo "amcoder [ a m ] \"words or codes to change\""
    echo "amcoder [ h ]"
    echo "a will change morse to text"
    echo "m will change text to morse"
    echo "everything to convert must be in quotes"
}

if (( $# > 2 ))
then
    usage
    exit 1
fi

case $1 in
    a) codes=($2)
        mstoas;;
    m) string="$2"
        astoms;;
    h) usage
        echoall;;        
    *) usage
        exit 1;;
esac

echo ${output[*]}

Last Updated ( Tuesday, 15 August 2006 )
 
Next >
Main Menu
Home
News
Blog
Links
HowTo
Code
Contact Me
Search
News Feeds
Gallery
Google Search
Videos
Gallery
template design by joomlashack