-
Notifications
You must be signed in to change notification settings - Fork 0
/
scramble.cpp
101 lines (84 loc) · 1.93 KB
/
scramble.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
/*
* take character, if uppercase, make lowercase and return
*/
char
myToLower(char c){
if( 'A' <= c && c <= 'Z'){
return c + ('a' - 'A');
}
return c;
}
/*
* take the argument string, change all uppercase chars to lowercase
* return the all lowercase string
*/
std::string
strToLower(std::string word){
char * c = &word[0];
char * itr = c;
while(*itr != '\0'){
*itr = myToLower(*itr);
itr++;
}
std::string str(c);
return str;
}
/*
* retrieve user input
* user enters a line of input(sentence)
* split word by word
* return a vector of the words entered by the user in order
*/
std::vector<std::string>
getUserInput(){
std::string prompt = "Please enter a sentence:";
std::string input;
std::string quit("q");
//get user input
std::cout << prompt << std::endl;
std::getline(std::cin, input);
std::vector<std::string> words;
char *word = strtok(&input[0], " ");
while(word != NULL){
std::string str(word);
str = strToLower(str);
words.push_back(str);
word = strtok(NULL, " ");
}
if(words.size() == 1 && words[0].compare(quit) == 0){
std::cout << "exiting..." << std::endl;
exit(0);
}
return words;
}
//send back all lower case. it's all scrambled anyways, no point in fussing over grammar/capitalization
std::vector<std::string>
sort(std::vector<std::string> words){
std::vector<std::string> newList;
for(unsigned int i = 0; i < words.size(); i++){
std::string w = words[i];
std::sort(w.begin(), w.end());
newList.push_back(w);
}
return newList;
}
int main(){
std::vector<std::string> words;
std::cout << "Quit by entering \"q\"" << std::endl;
while(1){
words = getUserInput();
words = sort(words);
for(unsigned int i = 0; i < words.size(); i++){
std::cout << words[i] + " ";
}
std::cout << std::endl;
}
return 0;
}