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 <boost/tokenizer.hpp>
#include <iterator>
#include <sstream>


int main() {
void string_to_vector ( const std::string & input , std::vector<std::string> & words ) {
std::string s = "rosetta code phrase reversal";
boost::tokenizer<> tok( input ) ;
std::cout << "Input : " << s << '\n'
for ( boost::tokenizer<>::iterator beg = tok.begin( ) ; beg != tok.end( ) ; ++beg )
<< "Input reversed : " << std::string(s.rbegin(), s.rend()) << '\n' ;
words.push_back( *beg ) ;
std::istringstream is(s);
}
std::vector<std::string> words(std::istream_iterator<std::string>(is), {});

int main( ) {
std::string startphrase ( "rosetta code phrase reversal" ) ;
std::cout << "Input : " << startphrase << '\n' ;
std::string local_copy ( startphrase ) ;
std::reverse ( local_copy.begin( ) , local_copy.end( ) ) ;
std::cout << "Input reversed : " << local_copy << '\n' ;
std::vector<std::string> words ;
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 ( std::string word : words ) {
for(auto w : words)
std::reverse ( word.begin( ) , word.end( ) ) ;
std::cout << std::string(w.rbegin(), w.rend()) << ' ';
std::cout << word << " " ;
std::cout << '\n'
<< "Original word order reversed : " ;
}
reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << '\n' ;
std::cout << "Original word order reversed : " ;
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>