Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include files
- #include <iostream>
- #include <string>
- #include <vector>
- #include <conio.h>
- #include <Windows.h>
- // define variables
- #define NUMLETTER 29
- #define SPECIAL 39
- #define MAX 1024
- #define quote ""
- using namespace std;
- // vectors being used
- vector<string>strVec;
- vector<string>upperLetters;
- vector<string>lowerLetters;
- vector<string>specialCharacters;
- // The pointer will be used to divide the sentence to characters (letters, whitespaces and such things)
- string userText, g_str;
- const char *pointer;
- // a lot of integers
- short int i, j, total_lower, total_upper, total_special, total_whitespace, length, runAgain;
- // Norwegian letters as hexcode
- // æ = 91, ø = 9B, å = 86
- // Æ = 92, Ø = 9D, Å = 8F
- // string arrays for comparison
- 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"};
- 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"};
- string special[] = {"|","§","!","#","¤","%","&","/","(",")","=","+","?","`","\"","¨","^","<",">","*",".",",","_",":",";", "'","@","£","$","{","[","]","}","´","~","€","+","-", quote};
- string whitespace[] = {" "};
- // Initializing the variables and emptying the vectors.
- void init()
- {
- // Everything should be set to 0
- i=j=total_lower=total_upper=total_special=total_whitespace=length=runAgain=0;
- // All the vectors of string type should be empty
- while(!strVec.empty())
- strVec.pop_back();
- while(!upperLetters.empty())
- upperLetters.pop_back();
- while(!lowerLetters.empty())
- lowerLetters.pop_back();
- while(!specialCharacters.empty())
- specialCharacters.pop_back();
- // This is not really necessary, but it is logic that the pointer to the user's text is set to NULL or 0.
- while(pointer != NULL)
- pointer = NULL;
- }
- void userDefinedFunction()
- {
- init();
- cout << "Your sentence: ";
- getline(cin, userText);
- pointer = userText.c_str();
- while(*pointer != '\0')
- {
- g_str = *pointer;
- strVec.push_back(g_str);
- *pointer++;
- length++;
- }
- cout << "\n\nOk, you wrote this sentence:\n";
- for(i=0; i<length; i++)
- {
- for(j=0; j<1; j++)
- if(strVec[i] == whitespace[j])
- {
- cout << endl;
- }
- else
- cout << strVec[i];
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<NUMLETTER; j++)
- {
- if(strVec[i].compare(upper[j]) == 0)
- {
- upperLetters.push_back(upper[j]);
- total_upper++;
- }
- }
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<NUMLETTER; j++)
- {
- if(strVec[i].compare(lower[j]) == 0)
- {
- lowerLetters.push_back(lower[j]);
- total_lower++;
- }
- }
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<SPECIAL; j++)
- {
- if(strVec[i].compare(special[j]) == 0)
- {
- specialCharacters.push_back(special[j]);
- total_special++;
- }
- }
- }
- for(i=0; i<length; i++)
- {
- for(j=0; j<1; j++)
- {
- if(strVec[i].compare(whitespace[j]) == 0)
- {
- total_whitespace++;
- }
- }
- }
- // Grammar....
- if(total_whitespace == 0)
- cout << "\n\n\nThe total of characters is: " << length;
- else if(total_whitespace == 1)
- cout << "\n\n\nThe total of characters (including the whitespace character) is: " << length;
- else if(total_whitespace > 1)
- cout << "\n\n\nThe total of characters (including the whitespace characters) is: " << length;
- if(total_upper > 0)
- {
- if(total_upper == 1)
- cout << "\n\nYou used a total of " << total_upper << " uppercase letter:\n";
- else
- cout << "\n\nYou used a total of " << total_upper << " uppercase letters:\n";
- }
- for(i=0; i<total_upper; i++)
- {
- if(i<total_upper-1)
- cout << upperLetters[i] << " ";
- else
- cout << upperLetters[i] << "\n";
- }
- if(total_lower > 0)
- {
- if(total_lower == 1)
- cout << "\n\nYou used a total of " << total_lower << " lowercase letter:\n";
- else
- cout << "\n\nYou used a total of " << total_lower << " lowercase letters:\n";
- }
- for(i=0; i<total_lower; i++)
- {
- if(i<total_lower-1)
- cout << lowerLetters[i] << " ";
- else
- cout << lowerLetters[i] << "\n";
- }
- if(total_special > 0)
- {
- if(total_special == 1)
- cout << "\n\nThere is a total of " << total_special << " special character being used:\n";
- else
- cout << "\n\nThere is a total of " << total_special << " special characters being used:\n";
- }
- for(i=0; i<total_special; i++)
- {
- if(i<total_special-1)
- cout << specialCharacters[i] << " ";
- else
- cout << specialCharacters[i] << "\n";
- }
- if(total_whitespace > 0)
- {
- if(total_whitespace == 1)
- cout << "\n\nThere is a total of " << total_whitespace << " whitespace character being used.";
- else
- cout << "\n\nThere is a total of " << total_whitespace << " whitespace characters being used.";
- }
- }
- // run() is being used in the main function, we just need to make it available for main() to get access to run()
- void run();
- int main()
- {
- HWND console = GetConsoleWindow();
- RECT r;
- GetWindowRect(console, &r);
- MoveWindow(console, 100, 60, 800, 600, TRUE);
- // Welcome to you!
- cout << "\nNow we will count some letters, white spaces and special characters.\n";
- cout << "Please enter a sentence and press Enter when you have written it.\n\n";
- // Time for input, counting and printing to the screen.
- userDefinedFunction();
- // Does the user want to try again? Yes, No, default: run again.
- run();
- return 0;
- }
- void clearScreen()
- {
- // Clear the screen to make it look more clean!
- system("CLS");
- }
- void run()
- {
- cout << "\n\nWould you like to run this program again? Y = yes, N = no.\nYour choice: ";
- runAgain = _getch();
- clearScreen();
- if(runAgain == 'y' || runAgain == 'Y')
- main();
- else if(runAgain == 'n' || runAgain == 'N')
- // do nothing
- ;
- else
- run();
- }
Advertisement
Add Comment
Please, Sign In to add comment