Created
April 29, 2013 01:16
-
-
Save someoneigna/5479164 to your computer and use it in GitHub Desktop.
Input to morse code converter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
#include<cstring> | |
#include<cctype> | |
const char *morse[]={" ","--..--",".-.-.-","..--..", // Space, comma, period and question mark | |
"-----",".----","..---","...--","....-",".....","-....","--...","---..","----.", // Numbers | |
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---", // Alphabeth | |
".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; // | |
const char latin[]=" ,.?0123456789abcdefghijklmnopqrstuvxyz"; | |
const char * ctom(const char c) | |
{ | |
int i; | |
for(i=0;i<strlen(latin);i++) | |
{ | |
if(tolower(c) == latin[i]) | |
return morse[i]; | |
} | |
return NULL; | |
} | |
std::string toMorse(const std::string input) | |
{ | |
std::string output; | |
std::string::const_iterator it = input.begin(); | |
for(;it != input.end(); it++) | |
{ | |
char *morse =(char*) ctom(*it); | |
if(morse) | |
output.append(ctom(*it)); | |
} | |
return output; | |
} | |
int main() | |
{ | |
std::string input; | |
std::cout<<"Input:"; | |
std::getline(std::cin,input); | |
std::cout<<std::endl<<"Output:"<<toMorse(input)<<std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment