int main () { std::string inputString; // The initial word/phrase entered by the user std::string command; // The command string, entered by the user int stringLen=0; // Length of string, max int nextAction=0; // What command the user has chosen int cmdArgument=0; // Argument to that command, if any char inputChars[MAX_STRING_LEN]; // A c-string, easier to manipulate // Get a word or phrase from user input inputString = getUserString(MAX_STRING_LEN); if (inputString.compare("") == 0 ) return(-1); // Copy the c++ string into a c-string array. // Using strncpy and adding the null isn't necessary if the string // is shorter than MAX_LEN, but it isn't harmful either. std::strncpy(inputChars, inputString.c_str(), MAX_STRING_LEN - 1 ); inputChars[MAX_STRING_LEN-1] = '\0'; // Print the menu of options and enter a loop std::cout << "How do you want to tweak this phrase?" <<std::endl; do { std::cout << "R#, L#, or rev: "; getline(std::cin, command); nextAction = parseCommand(command, cmdArgument); switch(nextAction) { case REV: reverseString(inputChars); std::cout << "Reverse: " << inputChars << std::endl; break; case LSHIFT: shiftString(inputChars, cmdArgument); std::cout << "Left " << cmdArgument << ": " << inputChars << std::endl; break; case RSHIFT: shiftString(inputChars, -(cmdArgument)); std::cout << "Right "<< cmdArgument << ": " << inputChars << std::endl; break; case QUIT: break; default: std::cout << "Didn't recognize that command, try again. (Type \"quit\" to quit)" << std::endl; break; } } while ( nextAction != QUIT ); }
/** stripSpaces: removes whitespace from a string * @param c - String to strip */ void stripSpaces(char * c) { char *space = " "; if (strncmp(space, c, 1) == 0) { int i = 0; while (c[i] == CHAR_SPACE) { i++; } shiftString(c, i, SIZE); } }