Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. void ToUper(char& c)
  7. {
  8.     if(c >= 'a' && c <= 'z')
  9.         c = c - ('a' - 'A');
  10.  
  11. }
  12. bool isAlpha(char c)
  13. {
  14.     if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  15.         return true;
  16.  
  17.     return false;
  18. }
  19. bool isNumber(char c)
  20. {
  21.     if(c >= '0' && c <= '9')
  22.         return true;
  23.  
  24.     return false;
  25. }
  26. void MorseCodeForAlpha(char c)
  27. {
  28.     const char alpha[] = {'A','B','C','D','E','F','G','H','I',
  29.                           'J','K','L','M','N','O','P','R',
  30.                           'S','T','U','V','W','X','Y','Z'};
  31.     const char* morse[] = {".-","-...","-.-.","-..",".","..-.",
  32.     "--.","....","..",".---","-.-",".-..","--","-.","---",".--.",
  33.     "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
  34.     };
  35.     for(int i=0; i<27; i++)
  36.     {
  37.         if(c == alpha[i])
  38.         {
  39.             cout << morse[i] << " ";
  40.         }
  41.     }
  42. }
  43. void MorseCodeForNumber(char c)
  44. {
  45.     const char number[] = {'0','1','2','3','4','5','6','7','8','9'};
  46.     const char* morse[] = { "-----",".----","..---","...--","....-",
  47.                             ".....","-....","--...","---..","----."};
  48.     for(int i=0; i<11; i++)
  49.     {
  50.         if(c == number[i])
  51.         {
  52.             cout << morse[i] << " ";
  53.         }
  54.     }
  55. }
  56. int main()
  57. {
  58.     char message[100];
  59.     cin.get(message,99);
  60.     int length = strlen(message);
  61.  
  62.     for(int i=0; i<length; i++)
  63.     {
  64.         if(isAlpha(message[i]))
  65.         {
  66.                 ToUper(message[i]);
  67.                 //cout << message[i] << endl;
  68.  
  69.                 MorseCodeForAlpha(message[i]);
  70.                 //cout <<"This is alpha" <<endl;
  71.         }
  72.         else if (isNumber(message[i]))
  73.         {
  74.                 MorseCodeForNumber(message[i]);
  75.                // cout << " This is number" << endl; continue;
  76.         }
  77.         else if(message[i] == ' ')
  78.         {
  79.             cout << "   "; continue;
  80.         }
  81.         else cout << "Bad input" << endl;
  82.     }
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement