Reverse a string array

I want to reverse a string in an array and also every alternating word in a string for example:

[INPUT]
The mouse was caught in a clothes peg.
[OUTPUT]
The esoum was thguac in a clothes gep. (every alternate (even) word reversed)
.gep sehtolc a ni thguac saw esuoum ehT (all string reversed).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
	const int MAX = 100; // max number of characters on one line
	char input[MAX];
	string reverse_word, text, word;
	int word_count(0), count = 0;
	cout << "Enter a sentence: " << endl;
	cin.getline(input, MAX, '\n');

	if (cin.fail())
	{cout << "Exceeded maximum number of characters (max 100)";}
	else
	{
		while (input[count] != '\0')
		{
			input[count];
			text += input[count];

			if (text[count] == ' ')
			{word_count++;}
			if (word_count % 2 == 0)
			{word = string(text.rbegin(), text.rend());}
			
			count++;
		}
		reverse_word = string(text.rbegin(), text.rend());
	}
	cout << reverse_word << endl;
	system("PAUSE");
	return 0;
}
Last edited on
you can use stringstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
	string text= "abc def ghi jkl.";
	string word;	
	stringstream ss;
	ss << text;
	int i=0;

	while (ss>>word) {		
		if(i%2==0) {
			cout << word << " ";
		}		
		else {
			// reverse word
			cout << word << " ";
		}
		i++;

	}
	
	return 0;
}
How would I incorporate that in an array string?
try
Topic archived. No new replies allowed.