Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <vector>
- bool ordered_word(std::string);
- int main(int argc, char* argv[]){
- //testing
- //std::cout << strcasecmp("A","b") << "\n";
- //std::cout << ordered_word("a")<< "\n";
- //stuff the longest words into a vector
- std::vector<std::string> longest;
- int longest_length = 0;
- //iterate over stdin
- for (std::string line; std::getline(std::cin,line);) {
- if (ordered_word(line)) {
- //case 1: new word is longer
- //clear previous results and add
- //new result. Also update our
- //longest length.
- int new_length = line.length();
- if (new_length > longest_length) {
- longest.clear();
- longest.push_back(line);
- longest_length = line.length();
- //case 2: new word is the same length
- //as previous. Add the new result
- //onto the results list.
- } else if (new_length == longest_length) {
- longest.push_back(line);
- }
- }
- }
- //print out the results.
- for (int i = 0; i < longest.size(); i ++) {
- std::cout << longest[i] << " ";
- }
- std::cout << "\n";
- }
- bool ordered_word(std::string word){
- //check for single-character word
- int len = word.length();
- if (len == 1) return true;
- //check for empty word (just in case)
- if (len == 0) return false;
- std::string first = word.substr(0, 1);
- for (int i = 1; i <= (len - 1); i++){
- std::string second = word.substr(i,1);
- //use c_str to get c equivalen char *
- if (strcasecmp(first.c_str(),second.c_str()) > 0) return false;
- first = second;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement