Phrase reversals: Difference between revisions
Content added Content deleted
(Added Elixir) |
(→{{header|C++}}: actually simpler without boost) |
||
Line 304: | Line 304: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
#include <iostream> |
|||
#include <vector> |
#include <vector> |
||
#include <algorithm> |
#include <algorithm> |
||
#include <string> |
#include <string> |
||
#include < |
#include <iterator> |
||
#include <sstream> |
|||
⚫ | |||
void string_to_vector ( const std::string & input , std::vector<std::string> & words ) { |
|||
⚫ | |||
boost::tokenizer<> tok( input ) ; |
|||
⚫ | |||
for ( boost::tokenizer<>::iterator beg = tok.begin( ) ; beg != tok.end( ) ; ++beg ) |
|||
⚫ | |||
words.push_back( *beg ) ; |
|||
std::istringstream is(s); |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
std::string local_copy ( startphrase ) ; |
|||
std::reverse ( local_copy.begin( ) , local_copy.end( ) ) ; |
|||
⚫ | |||
⚫ | |||
string_to_vector ( startphrase , words ) ; |
|||
//copy the vector with the original words to reverse their order later |
|||
std::vector<std::string> original_words ( words ) ; |
|||
//reverse each word in the string |
|||
std::cout << "Each word reversed : " ; |
std::cout << "Each word reversed : " ; |
||
for |
for(auto w : words) |
||
std:: |
std::cout << std::string(w.rbegin(), w.rend()) << ' '; |
||
std::cout << '\n' |
|||
⚫ | |||
} |
|||
reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " ")); |
|||
std::cout << '\n' ; |
|||
⚫ | |||
for ( std::vector<std::string>::const_reverse_iterator cri = original_words.rbegin( ) ; |
|||
cri != original_words.rend( ) ; cri++ ) { |
|||
std::cout << *cri << " " ; |
|||
} |
|||
std::cout << '\n' ; |
std::cout << '\n' ; |
||
return 0 ; |
|||
} |
} |
||
</lang> |
</lang> |