Implement a C++ program to assist users in the creation of good passwords. When invoked, the program prompts the user to enter a data phrase on one line of input. The phrase may include any printable ASCII characters, including spaces and tabs, but excluding the newline character, '\n', which is used to delimit the end of the phrase. After reading the phrase and checking that it is nonempty, the program displays the following menu.
String Slicer Menu ------------------ P PRINT the phrase to the screen S SLICE one letter from each word T TABULATE all slices in adjacent columns Q QUIT the programNext, and, after every successful completion of a user request, the program displays the following short prompt:
PSTQ>The sequence of prompts and responses for each of the above options is listed in detail below.
Whitespace refers to any sequence of any of the following characters: ' ' '\t' '\n' '\r' '\f' . To test whether a character is whitespace, you may compare to the above charaters directly, or you may use the bool std::isspace(char) function from the cctype library.
In text processing, a word is any contiguous sequence of nonwhitespace characters. Hence, words in a string are separated from each other by whitespace. But the last word in a string might not have whitespace after it.
The driver (main() function) should be kept short. Menu options S and T should each be handled by at least one separate function. To support these functions, you will find it useful to implement additional functions for performing the following tasks, among others.
Recall that the stream extraction operator >> leaves any '\n' character trailing its input in the input stream for the next input operation to handle. Hence, the following code fragment will fail to correctly input a line of text into the string s:
string s; int i; cout << "Enter an integer, then enter a string: " cin >> i; // This operation leaves the '\n' behind! getline(cin, s); // Error: s does not receive any data!To remove a known and unwanted newline character left behind by the >> operator, use the istream::ignore(int, char) function:
cin >> i; // This operation leaves the '\n' behind! cin.ignore(1000, '\n'); // Discards the unwanted '\n' getline(cin, s); // Works as intended.The number 1000 preceding the '\n' specifies an upper limit on how many characters preceding the '\n' to ignore.
Remember to ensure that, whenever you traverse any part of a string, you never attempt to access memory past the last stored character or before the first stored character in the string. Use the string::length() or string::size() function to prevent this very common error.
$ slice Enter a one-line phrase: individuals in a hierarchy rise to their levels of incompetence String Slicer Menu ------------------ P PRINT the phrase to the screen S SLICE one letter from each word T TABULATE all slices in adjacent columns Q QUIT the program PSTQ> p individuals in a hierarchy rise to their levels of incompetence PSTQ> s Which letter? Enter the slice index (1-12): 1 Requested slice: iiahrttloi PSTQ> s Which letter? Enter the slice index (1-12): 2 Requested slice: nnaiiohefn PSTQ> s Which letter? Enter the slice index (1-12): 0 Which letter? Enter the slice index (1-12): 14 Which letter? Enter the slice index (1-12): -54 Which letter? Enter the slice index (1-12): 7 Requested slice: diacsthloe PSTQ> p individuals in a hierarchy rise to their levels of incompetence PSTQ> t i n d i v i d u a l s i i n i n i n i n i n i n a a a a a a a a a a a a h i e r a r c h y h i e r i s e r i s e r i s e t o t o t o t o t o t o t h e i r t h e i r t h l e v e l s l e v e l s o f o f o f o f o f o f i n c o m p e t e n c e PSTQ> ? String Slicer Menu ------------------ P PRINT the phrase to the screen S SLICE one letter from each word T TABULATE all slices in adjacent columns Q QUIT the program PSTQ> q $