Jump to content

How would I reverse the words in a string in c++?

RandomRestore

I am unsure how to reverse the words in a string. I'm trying to find the number of words that there are with the number of spaces(+1) [assuming that they don't add a space before or after the sentence]. Then from there, I want to somehow use the number of words to create a script that reverses the words in a sentence [assuming that the sentence does have a "." at the end]. 

 

Sample Input:

My name is Edward

 

Sample Output:
Edward is name My

 

Here is my code so far: http://cpp.sh/4nzz

 

Thanks for reading, a beginner like me really appreciates your help.

CPU: AMD A8-6600K 3.9GHz(OCed to 4.5GHz) Quad-Core Processor
CPU Cooler: Cooler Master Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
Motherboard: MSI A88XM-E45 Micro ATX FM2+ Motherboard
Memory: Kingston HyperX Fury Blue 8GB (2 x 4GB) DDR3-1600 Memory 
Storage: Kingston SSDNow V300 Series 120GB 2.5" Solid State Drive
Storage: Seagate Barracuda 120GB 3.5" Internal Hard Drive
Video Card: EVGA GeForce GTX 760 4GB Dual FTW ACX Video Card
Case: Silverstone TJ09-BW ATX Full Tower Case
Power Supply: EVGA SuperNOVA NEX 650W 80+ Gold Certified Fully-Modular ATX Power Supply

Link to comment
Share on other sites

Link to post
Share on other sites

use a vector and make a loop to reverse it

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>
#include <string>
#include <sstream>

int main()
{
	std::string Input, Output;
	std::getline(std::cin, Input);			//Read input line from user
	std::istringstream iss(Input);			//Create istringstream from input string
 	for (std::string Word; iss >> Word; )		//Extract word per word from istringstream...
		Output = Word + ' ' + Output;		//And put new word first and append rest...

	std::cout << Output << std::endl;
	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

From <algorithm>, use std::reverse:

reverse.cpp

#include <iostream>
#include <string>
#include <algorithm>

int main()
{

    //Using std::reverse to reverse a string in place
    std::string myString{"Hello, this is a string"};
    std::cout << "myString = " << myString << std::endl; //Original string
    std::reverse(myString.begin(), myString.end()); //Reverse it in place
    std::cout << "myString = " << myString << std::endl; //Reveresed string

    return 0;
}

Output:

pinguinsan@chromebook-samus:~$ ./reverse 
myString = Hello, this is a string
myString = gnirts a si siht ,olleH

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Pinguinsan said:

Output:


pinguinsan@chromebook-samus:~$ ./reverse 
myString = Hello, this is a string
myString = gnirts a si siht ,olleH

 

That's not what was requested, he wants to reverse the words, not all characters.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×