Reverse words in a sentence

I am trying to write a program in which i enter sentences and then gives the reversed output

Eg: -

INPUT

Enter the number of sentences
3

This is a sentence

Program

You are great

OUTPUT

sentence a is This
Program
great are You



I wrote the following code but its failing when im trying to enter a sentence


#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void str(char*, int);
void main()
{
char *word[25];
cout<<"Enter the number of test cases"<<endl;
int n,ctr=0;
cin>>n;
while(ctr<n)
{
gets(word[ctr]);
}
int i=0;
while(i<=ctr)
{
str(word[i],n);
i++;
}
}

void str(char *st, int n)
{
int len,ctr=0;
len=strlen(st);
char *st2;
st2[len]='\0';
int j;
for(int i = 0; st[i]!='\0'; i++)
{
if(st[i]==' ')
{
ctr=i;
for( j = len-1; j>=0; j--)
{
st2[j]=st[ctr];
ctr--;
}
st2[j-1]=' ';
}
}
for(int i = 0; i < n; i++)
{
cout<<st2[i]<<endl;
}
}




Your code would be easier to read if it were in code brackets.
Just choose the <> symbol on the right side of the text editor under Format:
Last edited on
you should really put it in code format so it is easier to read.
This here hasn't been thoroughly tested and I'm still fairly new to C++, but this should do the job just fine. Also, I recommend you use vectors instead of C_Arrays, since they are FAR easier to use and offer the same functionality.

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>
#include <algorithm>
#include <vector>

std::string reverseSentence(std::string sentence) {
	std::string reversedSentence;
	int words{1};
	std::vector<std::string> word(words);
	for (size_t i = 0; i < sentence.length(); i++) {
		if (sentence[i] != ' ') {
			word[words - 1] += sentence[i];
		}else {
			words++;
			word.resize(words);
		}
	}
	std::reverse(word.begin(), word.end());
	for (auto &curWord : word) {
		reversedSentence.append(curWord + " ");
	}
	return reversedSentence;
}

int main() {
	std::string sentence = "This is a sentence";
	std::cout << reverseSentence(sentence) << "\n";
        std::cin.get();
	return 0;
}
Last edited on
I am new to this site , next time ill put it in <>
Thanks anyway
too much code for this task.



1
2
3
4
5
	reverseString(string words)
{
	for(int i=words.size();i>=0;i--)
	cout<<words[i];
}
I believe code rookie wanted an output similar to the following:

sentence a is This


and not simply reversed.
Also, if you wanted to reverse a string, you could just call the reverse() function.
@Cody0023
On a side note, that is accessing out of bounds on the vector.

@code rookie
A few things - don't use void main (prefer instead int main), you are modifying pointers and just running over memory all over the place (you need to allocate memory for your char pointers). Also, don't use gets - it is dangerous.

As for the problem, you could use stringstream, copy and iterators (I happened to do this the other day):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <deque>

void reversedString(const std::string& in) {
    std::istringstream iss (in);
    std::deque<std::string> words;
    std::copy(std::istream_iterator<std::string>(iss), 
              std::istream_iterator<std::string>(), 
              std::front_inserter(words));
    std::copy(std::begin(words), std::end(words), 
              std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << std::endl;
}

int main() {
    reversedString("This is a sentence");
    reversedString("Program");
    reversedString("You are great");
    return 0;
}
sentence a is This 
Program 
great are You

http://coliru.stacked-crooked.com/a/8b6517b563539dea
Last edited on
Topic archived. No new replies allowed.