MegaTor

Count letters

Feb 3rd, 2013
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. // include files
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <conio.h>
  6. #include <Windows.h>
  7.  
  8. // define variables
  9. #define NUMLETTER 29
  10. #define SPECIAL 39
  11. #define MAX 1024
  12. #define quote ""
  13.  
  14. using namespace std;
  15.  
  16. // vectors being used
  17. vector<string>strVec;
  18. vector<string>upperLetters;
  19. vector<string>lowerLetters;
  20. vector<string>specialCharacters;
  21.  
  22. // The pointer will be used to divide the sentence to characters (letters, whitespaces and such things)
  23. string userText, g_str;
  24. const char *pointer;
  25.  
  26. // a lot of integers
  27. short int i, j, total_lower, total_upper, total_special, total_whitespace, length, runAgain;
  28.  
  29. // Norwegian letters as hexcode
  30. // æ = 91, ø = 9B, å = 86
  31. // Æ = 92, Ø = 9D, Å = 8F
  32.  
  33. // string arrays for comparison
  34. string upper[] = {"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","\x92","\x9D","\x8F"};
  35. string lower[] = {"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","\x91","\x9B","\x86"};
  36. string special[] = {"|","§","!","#","¤","%","&","/","(",")","=","+","?","`","\"","¨","^","<",">","*",".",",","_",":",";", "'","@","£","$","{","[","]","}","´","~","€","+","-", quote};
  37. string whitespace[] = {" "};
  38.  
  39.  
  40. // Initializing the variables and emptying the vectors.
  41. void init()
  42. {
  43.     // Everything should be set to 0
  44.     i=j=total_lower=total_upper=total_special=total_whitespace=length=runAgain=0;
  45.  
  46.     // All the vectors of string type should be empty
  47.     while(!strVec.empty())
  48.         strVec.pop_back();
  49.     while(!upperLetters.empty())
  50.         upperLetters.pop_back();
  51.     while(!lowerLetters.empty())
  52.         lowerLetters.pop_back();
  53.     while(!specialCharacters.empty())
  54.         specialCharacters.pop_back();
  55.  
  56.     // This is not really necessary, but it is logic that the pointer to the user's text is set to NULL or 0.
  57.     while(pointer != NULL)
  58.         pointer = NULL;
  59. }
  60.  
  61. void userDefinedFunction()
  62. {
  63.     init();
  64.  
  65.     cout << "Your sentence: ";
  66.     getline(cin, userText);
  67.  
  68.     pointer = userText.c_str();
  69.  
  70.     while(*pointer != '\0')
  71.     {
  72.         g_str = *pointer;
  73.         strVec.push_back(g_str);
  74.         *pointer++;
  75.         length++;
  76.     }
  77.  
  78.     cout << "\n\nOk, you wrote this sentence:\n";
  79.     for(i=0; i<length; i++)
  80.     {
  81.         for(j=0; j<1; j++)
  82.             if(strVec[i] == whitespace[j])
  83.             {
  84.                 cout << endl;
  85.             }
  86.             else
  87.                 cout << strVec[i];
  88.     }
  89.  
  90.     for(i=0; i<length; i++)
  91.     {
  92.         for(j=0; j<NUMLETTER; j++)
  93.         {
  94.             if(strVec[i].compare(upper[j]) == 0)
  95.             {
  96.                 upperLetters.push_back(upper[j]);
  97.                 total_upper++;
  98.             }
  99.         }
  100.     }
  101.  
  102.     for(i=0; i<length; i++)
  103.     {
  104.         for(j=0; j<NUMLETTER; j++)
  105.         {
  106.             if(strVec[i].compare(lower[j]) == 0)
  107.             {
  108.                 lowerLetters.push_back(lower[j]);
  109.                 total_lower++;
  110.             }
  111.         }
  112.     }
  113.  
  114.     for(i=0; i<length; i++)
  115.     {
  116.         for(j=0; j<SPECIAL; j++)
  117.         {
  118.             if(strVec[i].compare(special[j]) == 0)
  119.             {
  120.                 specialCharacters.push_back(special[j]);
  121.                 total_special++;
  122.             }
  123.         }
  124.     }
  125.  
  126.     for(i=0; i<length; i++)
  127.     {
  128.         for(j=0; j<1; j++)
  129.         {
  130.             if(strVec[i].compare(whitespace[j]) == 0)
  131.             {
  132.                 total_whitespace++;
  133.             }
  134.         }
  135.     }
  136.  
  137.  
  138.     // Grammar....
  139.     if(total_whitespace == 0)
  140.         cout << "\n\n\nThe total of characters is: " << length;
  141.     else if(total_whitespace == 1)
  142.         cout << "\n\n\nThe total of characters (including the whitespace character) is: " << length;
  143.     else if(total_whitespace > 1)
  144.         cout << "\n\n\nThe total of characters (including the whitespace characters) is: " << length;
  145.  
  146.  
  147.     if(total_upper > 0)
  148.     {
  149.         if(total_upper == 1)
  150.             cout << "\n\nYou used a total of " << total_upper << " uppercase letter:\n";
  151.         else
  152.             cout << "\n\nYou used a total of " << total_upper << " uppercase letters:\n";
  153.     }
  154.     for(i=0; i<total_upper; i++)
  155.     {
  156.         if(i<total_upper-1)
  157.             cout << upperLetters[i] << " ";
  158.         else
  159.             cout << upperLetters[i] << "\n";
  160.     }
  161.  
  162.     if(total_lower > 0)
  163.     {
  164.         if(total_lower == 1)
  165.             cout << "\n\nYou used a total of " << total_lower << " lowercase letter:\n";
  166.         else
  167.             cout << "\n\nYou used a total of " << total_lower << " lowercase letters:\n";
  168.     }
  169.     for(i=0; i<total_lower; i++)
  170.     {
  171.         if(i<total_lower-1)
  172.             cout << lowerLetters[i] << " ";
  173.         else
  174.             cout << lowerLetters[i] << "\n";
  175.     }
  176.  
  177.     if(total_special > 0)
  178.     {
  179.         if(total_special == 1)
  180.             cout << "\n\nThere is a total of " << total_special << " special character being used:\n";
  181.         else
  182.             cout << "\n\nThere is a total of " << total_special << " special characters being used:\n";
  183.     }
  184.     for(i=0; i<total_special; i++)
  185.     {
  186.         if(i<total_special-1)
  187.             cout << specialCharacters[i] << " ";
  188.         else
  189.             cout << specialCharacters[i] << "\n";
  190.     }
  191.     if(total_whitespace > 0)
  192.     {
  193.         if(total_whitespace == 1)
  194.             cout << "\n\nThere is a total of " << total_whitespace << " whitespace character being used.";
  195.         else
  196.             cout << "\n\nThere is a total of " << total_whitespace << " whitespace characters being used.";
  197.     }
  198. }
  199.  
  200. // run() is being used in the main function, we just need to make it available for main() to get access to run()
  201. void run();
  202.  
  203. int main()
  204. {
  205.     HWND console = GetConsoleWindow();
  206.     RECT r;
  207.     GetWindowRect(console, &r);
  208.     MoveWindow(console, 100, 60, 800, 600, TRUE);
  209.  
  210.     // Welcome to you!
  211.     cout << "\nNow we will count some letters, white spaces and special characters.\n";
  212.     cout << "Please enter a sentence and press Enter when you have written it.\n\n";
  213.  
  214.     // Time for input, counting and printing to the screen.
  215.     userDefinedFunction();
  216.  
  217.     // Does the user want to try again? Yes, No, default: run again.
  218.     run();
  219.     return 0;
  220. }
  221.  
  222. void clearScreen()
  223. {
  224.     // Clear the screen to make it look more clean!
  225.     system("CLS");
  226. }
  227.  
  228. void run()
  229. {
  230.     cout << "\n\nWould you like to run this program again? Y = yes, N = no.\nYour choice: ";
  231.     runAgain = _getch();
  232.  
  233.     clearScreen();
  234.  
  235.     if(runAgain == 'y' || runAgain == 'Y')
  236.         main();
  237.     else if(runAgain == 'n' || runAgain == 'N')
  238.         // do nothing
  239.         ;
  240.     else
  241.         run();
  242. }
Advertisement
Add Comment
Please, Sign In to add comment