//
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Six, Easy Supp - question 1 [worth 3pts],  7/24/2002
//
//  prints out fingers used in touch typing
// 


#include <iostream>
#include <iomanip>
#include <cctype>

int const MAXSTR    = 80;
int const CHARRANGE = ('Z' - 'A' + 1);

//
//  Get a string [str] of length maxLength
//
void getString(char * query, char * str, int maxLength)
{
	cout << query;							// ask query
	
	cin >> ws;								// remove all the white spaces
	cin.getline(str, maxLength);			// read characters until \n MAXSTR
}


void setBit(int& bits, int val)
{
	bits |= (1 << val);
}


void clearBit(int& bits, int val)
{
	bits &= ((1 << val) ^ 255);
}


bool getBit(int bits, int val)
{
	return (bits & (1 << val) );
}


void storeUpperCase(int& bits, char* str)
{
	while (*str) {
		if (*str >= 'A' && *str <= 'Z') {
			setBit(bits, (*str - 'A') );
		}
		str++;
	}
}


void printBitset(int bits)
{
	cout << "{ ";
	for (int i = 0; i < CHARRANGE; i++) {
		if (getBit(bits, i)) {
			cout << char (i + 'A') << " ";
		}
	}
	cout << "}" << endl;
}


void printHandsUsed(char* str)
{
	bool used[2][4];
	int hands[2][4];
	hands[0][0] = 0;
	
	// init 'hands' array to 0
	for (int j = 0; j < 2; j++) {
		for (int i = 0; i < 4; i++) {
			hands[j][i] = 0;
		}
	}
	
	// data for hands
	storeUpperCase(hands[0][0], "RTFGBV");
	storeUpperCase(hands[0][1], "EDC");
	storeUpperCase(hands[0][2], "WSX");
	storeUpperCase(hands[0][3], "QAZ");	
	
	storeUpperCase(hands[1][0], "YUHJNM");
	storeUpperCase(hands[1][1], "IK");
	storeUpperCase(hands[1][2], "OL");
	storeUpperCase(hands[1][3], "P");
	

	// init 'used' array to false
	for (int j = 0; j < 2; j++) {
		for (int i = 0; i < 4; i++) {
			used[j][i] = false;
		}
	}
	
	// search through string
	while (*str) {
		if (isalpha(*str)) {
			
			*str = toupper(*str);					// convert to upper
			
			for (int j = 0; j < 2; j++) {
				for (int i = 0; i < 4; i++) {
					if (getBit(hands[j][i], *str - 'A')) {	// test key
						used[j][i] = true;
					}
				}
			}
		}
		str++;
	}
	
	cout << "Fingers exercised:" << endl;
	
	// read bool array
	for (int j = 0; j < 2; j++) {					
		for (int i = 0; i < 4; i++) {
			if (used[j][i]) {						// print fingers
				if (! j) {
					cout << "  L ";
				} else {
					cout << "  R ";
				}
				cout << i + 2 << endl;
			}
		}
	}
}


int main()
{
	char str[MAXSTR];
	
	getString("Type a line of input: ", str, MAXSTR);
	
	printHandsUsed( str );
	
	exit( 0 );
}
