Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
program MorseCoding;
function checkUserInput(userString: string): boolean;
var
valid: boolean;
count: integer;
begin
valid := true;
for count := 1 to length(userString) do
if ((ord(userString[count]) < 65) or (ord(userString[count]) > 90)) and (userString[count] <> ' ') then
valid := false;
checkUserInput := valid;
end;
function getCharacterCode(character: char): string;
const
translationArray: array [0..25] of string = ('.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');
begin
if character = ' ' then
getCharacterCode := '| '
else
begin
getCharacterCode := translationArray[ord(character) - 65] + ' ';
end;
end;
procedure main();
var
userString, codedString: string;
count: integer;
begin
write('Enter a string: ');
readln(userString);
if checkUserInput(upcase(userString)) then
begin
for count := 1 to length(userString) do
begin
codedString := concat(codedString, getCharacterCode(upcase(userString[count])));
end;
writeln(codedString);
end
else
writeln('Invalid input!');
end;
begin
main();
readln;
end.
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more