I am trying to solve this problem.
Write a program that reads one line of text(less than 50 words) and then prints it with each word reversed. For example:
Input: Today is Tuesday
Output: Tuesday is today
And all I'd think of is putting the words in the sentence into an array, and then printing them out from the biggest index to the smallest index. So I write this program, but it won't work. Please help me fix it, thanks so much.
I tried to let it print the words with the index number and I found out that the last "box" of the array stored the whole sentence. But I don't know what's wrong. I don't know where(which line of code) it stored that sentence.
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cout<<"Put in your sentence: ";
getline(cin,str);
string strWords[10];
int counter = 0;
for (int i = 0; i<str.length(); i++){
if (str[i] == ' ')
counter++;
else
strWords[counter] += str[i];
}
int j=9;
do{
cout<<j<<": "<<strWords[j]<<" ";
j--;
}
while(j>=0);
return 0;
}
example:
input:
Put in your sentence: WE are the people.
output:
9: WE are the people. 8: 7: 6: 5: 4: 3: people. 2: the 1: are 0: WE