Reverse alternating words in a string

I'm trying to create a small program to reverse every alternate words in a string.

For example: The mouse was caught in a peg.
OUTPUT: The esoum was thgauc in a peg.

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
#include<iostream> 
#include<string>
using namespace std;

int main()
{

	string  sentence, reversed_sentence, buffer;
	int no_words(0);
	cout << "Enter a sentence: ";
	cin >> sentence;
	for (int c = 0; c <= sentence.size(); c++)
	{
		if (isalpha(sentence[c]))
		{
			buffer += sentence[c];
		}
			else if (isspace(sentence[c]))
			{
			no_words++;
			reversed_sentence += buffer;
			buffer = "";
			}
		if (no_words % 2 == 0)
		{

		}
	}
	cout << no_words + 1 << endl << endl;
	system("PAUSE");
	return 0;
}
Last edited on
Aha, and what's your question, please?
How do you reverse a string for every alternate (even) word?
1
2
3
4
if (no_words % 2 == 0)
		{

		}

I'm trying to program it to reverse every even word if it satisfies the if statement above.

e.g.

The mouse was is caught in a peg.
The esouse was si caught ni a gep.

The underline words are the ones that need to be reversed.

I can reverse the whole string such as using this:

reversed_sentence = string(sentence.rbegin(),sentence.rend());
Last edited on
Try looping over the world from right to left instead from left to right.

1
2
3
4
for (int i = word.size(); i >= 0; i--)
{
    // ...
}
Last edited on
I've amended that and few other things but I get no output.


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
#include<iostream> 
#include<string>
using namespace std;

int main()
{

	string  sentence, reversed_sentence, buffer;
	int no_words(0);
	cout << "Enter a sentence: ";
	cin >> sentence;
	for (int c = sentence.size(); c >= 0; c--)
	{
		if (sentence[c] >= 'a' && sentence[c] <= 'z'
			||
			sentence[c] >= 'A' && sentence[c] <= 'Z')
		{
			buffer += sentence[c];
		}
		if (sentence[c] == ' ')
			{
			no_words++;
			reversed_sentence = string(buffer.rbegin(),buffer.rend());
			buffer = ' ';
			}
	}
	cout << reversed_sentence << endl << endl;
	system("PAUSE");
	return 0;
}


Basically, its working from right to left, one character at a time, if its a character its added to the buffer, if it finds a space it increments no_words and reverses the word peg to gep in this case (ignoring that the fact that I need to only reverse the alternate (even) words).
Last edited on
Have a look at http://www.cplusplus.com/reference/string/. Maybe you'll find some method to manipulate your strings as expected. Look f.e. for a method called insert or you'll try http://www.cplusplus.com/reference/algorithm/reverse/?
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
#include<iostream> 
#include<string>
using namespace std;

int main()
{
	string text, result, temp;
	int word_count(0);
	cout << "Enter a sentence: ";
	cin >> text;
	for (int c = 0; c <= text.size(); c++) // character by character (left to right)
	{
		if (text[c] >= 'a' && text[c] <= 'z' // if chracter is a letter
			           ||
		    text[c] >= 'A' && text[c] <= 'Z')
		{
			temp += text[c];	// add to contents of temp
		}
		if (text[c] == ' ')	// if chracter is a space
			{
			word_count++;
			if (word_count % 2 == 0)	// determine even word in a string
				{
					result += string(temp.rbegin(),temp.rend()); // reverse word, 
                                                                                  //concatinate word
				}
			result += temp;			// concatenate non-reversed word
			temp.clear();			// empty temp for next loop
			}
	}
	cout << "New sentence: " <<  result << endl << endl;
	system("PAUSE");
	return 0;
}


Here's my new code - but still outputs nothing.
Last edited on
Topic archived. No new replies allowed.