Easy–medium · strings
Reverse the Words
cin >> x reads one word and stops at the first space. Sentences need a different tool — and once you have the whole line, splitting it into words is just a loop with two moving indices.
At a glanceRead the line with <code>cin.getline</code>, cut it into words on the spaces, store them, then print the words from last to first, single-space separated.
Problem
Input: One line of text: 1 to 100 words, separated by one or more spaces. Words contain no spaces; the line is at most 200 characters.
Output: The same words in reverse order, separated by single spaces, followed by a newline.
Samples:
| Input | Output |
|---|---|
the quick brown fox | fox brown quick the |
hello | hello |
a b c | c b a |
What flips is the order of the words; the letters inside each word stay put — you are reversing the sequence of words, not each word's characters (that was Reverse a String).
Your solution
Custom input for "Run my input"
Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.
Stuck? Hints
Hint 1 — You need the whole line, not one word
cin >> word stops at the first space, so with the quick brown fox you would only ever get the. cin.getline(line, 205) reads everything up to the newline — spaces included — which is exactly what a sentence needs.
Hint 2 — Two indices walk the line
Keep an index i scanning forward. When you are on a letter, remember where the word started with a second index and copy until you hit a space or '\0'. That start/end pair is one word. Store each word in a 2D char array like char words[50][105] — row k is word number k.
Hint 3 — Print the rows backwards
If you stored count words, the last one lives in row count - 1. A for loop from count - 1 down to 0 prints them in reverse; print a space between words, not after the last one. Runs of several spaces in the input collapse to one space in the output — fine, the judge only wants the word sequence.
Solution (try it first!)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char line[205];
cin.getline(line, 205);
char words[50][105];
int count = 0;
int i = 0;
while (line[i] != '\0') {
while (line[i] == ' ') i++;
if (line[i] == '\0') break;
int j = 0;
while (line[i] != '\0' && line[i] != ' ') {
words[count][j] = line[i];
i++;
j++;
}
words[count][j] = '\0';
count++;
}
for (int k = count - 1; k >= 0; k--) {
cout << words[k];
if (k > 0) cout << " ";
}
cout << endl;
return 0;
}
This split-and-store loop is the same machinery Count the Words uses to count instead of store — one action changes. A worthwhile stretch: while printing, also count the words and output the count on a second line. From here, the next step in string land is walking a string from both ends at once in Palindrome String.
Deep dive
Why this works
A sentence is not one token — it is a sequence of tokens. The pattern is scan, slice, store: walk the line with one index, slice out each word with a second, keep them in an array, and from then on you own the words in any order you like. Split-then-process returns in every text task, from counting words to parsing CSV.
The cost
One pass to split plus one pass to print: O(L) time for a line of length L, and O(L) extra space for the stored words. With L ≤ 200 that is nothing — the same code would handle a 100,000-character line.
Where it goes wrong
- Reading with
cin >> sinstead ofcin.getline— you get exactly one word, every multi-word test fails, and nothing looks wrong until you notice the missing spaces. - Forgetting the
'\0'after copying a word into its row. Without the terminator,cout << words[k]keeps printing into whatever sits next in memory.
Next step
The same scan-and-slice loop counts words instead of storing them in Count the Words — change what you do with each slice and it is a new problem.
Two things to remember
cin >>reads one word;cin.getlinereads the whole line — pick by what the input is, not by habit.- Reversing words = store them all, then print the rows backwards.
🐞 This version fails — open it and try
Copies the line through untouched — the words never move
#include <iostream>
using namespace std;
int main() {
char line[205];
cin.getline(line, 205);
cout << line << endl;
return 0;
}