clueless coding // TODO: be smarter

LeetCode Reverse Words in a String II: CPP Solution



TL;DR Code Solution



void reverseWord(string& s, int left, int right) {
    while (left < right) {
        char temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        left++;
        right--;
    }
}

void reverseWords(string &s) {

    reverseWord(s, 0, s.length() - 1);

    int left = 0;
    for (int i = 0; i < s.length(); i++) {
        if (isblank(s[i])) {
            reverseWord(s, left, i - 1);
            left = i + 1;
        }
    }

    reverseWord(s, left, s.length() - 1);

}