Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created April 29, 2018 05:33
Show Gist options
  • Save Caaz/500cc60c052389e805ed813e2872c387 to your computer and use it in GitHub Desktop.
Save Caaz/500cc60c052389e805ed813e2872c387 to your computer and use it in GitHub Desktop.
messing with vectors
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <sstream>
using namespace std;
bool ci_compare (char a, char b) {return tolower(a)<tolower(b);}
void display(vector<string> words) {
for (string word : words) cout << word << ' ';
cout << endl;
}
int main() {
// Get our sentence
string sentence;
cout << "Your sentence: ";
getline(cin,sentence);
// Split it by whitespace using regex.
regex ws("\\s+");
vector<string> words{
std::sregex_token_iterator(sentence.begin(), sentence.end(), ws, -1), {}
};
// display words in default order
display(words);
// reverse the words
reverse(words.begin(), words.end());
display(words);
// return words to normal
reverse(words.begin(), words.end());
// sort the letters in individual words
for (string word : words){
// Turn the word into a character vector
vector<char> chars(word.begin(), word.end());
// sort the character vector, case insensitively.
sort(chars.begin(), chars.end(), ci_compare);
// display the new character vector
for (char letter : chars) cout << letter;
cout << " ";
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment