I am working on a bonus question for school. By using #include <iostream> and #include <string>, I need the user to input a sentence getline(cin,sentence).
Move the first word of that sentence to the end.
example:
Enter a line of text.
MAC101 is the class
rephrased that line to read:
is the class MAC101
Look at the reference page for std::string: http://www.cplusplus.com/reference/string/string/
There are lots of useful functions such as find(), replace(), substr(), insert(), erase() etc. Think about what it is you need to do, step by step, and some of these could be extremely useful.
#include <iostream>
int main()
{
std::string userinput; //Original input
std::string firstword; //Broken into two parts, the first word then the rest of the string
std::string rest;
do //To avoid any funny business of adding spaces in front of the sentence
{
std::cout << "Enter a sentence: ";
getline(std::cin, userinput);
}while(isspace(userinput[0])); //Instead of userinput[0] == ' ' we use isspace
int sizestr = userinput.length(); //Get size of original string
int savedspot = 0; //Instead of losing this value inside the for loop, we hang onto it
for(; savedspot < sizestr; ++savedspot) //This loop is used to get the first word in the string
{
if(!isspace(userinput[savedspot])) //We check to see if it goes over any spaces, if it does, the loop is done and the word is stored
{
firstword += userinput[savedspot];
}
elsebreak;
}
for(; savedspot < sizestr; ++savedspot) //We continue the loop for the rest of the string
{
rest += userinput[savedspot];
}
rest = rest.substr(1).append(rest.substr(0,1)); //Mandatory to avoid an ugly space that will always be left over at the start of the modified sentence
//For example if the above line is not included you will have " world hello" instead of "world hello"
std::cout << rest << firstword; //Glue the pieces together, backwards
return 0;
}
/*
OUTPUT
Enter a sentence: hello how are you world
how are you world hello
Process returned 0 (0x0) execution time : 3.060 s
Press any key to continue.
*/
I am 100% sure there is a better way to do this process but we all have to start somewhere I guess.
At this stage I think there are two things to consider. One is to get any sort of solution which works. The other is to focus on what you learn while getting there - as R.L.Stevenson said, "To travel hopefully is a better thing than to arrive".
I am 100% sure there is a better way to do this process
- what i originally had in mind was to find the first whitespace in the string, which gives us the location of the end of the first word (assuming the sentence doesn't start with any spaces). Next, copy that word as a substring, erase that word and associated whitespace. Finally append one space and our saved word onto the string.
one of my solutions looked like this, which followed that description
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string text = "move \t the first word in a line of text to \t the end" ;
std::cout << text << '\n' ;
// locate the first white space
constauto first_ws = std::find_if( text.begin(), text.end(), []( char c ) { return std::isspace(c) ; } ) ;
constauto next_non_ws = std::find_if( first_ws, text.end(), []( char c ) { return !std::isspace(c) ; } ) ;
if( next_non_ws != text.end() ) // if text contains more than one word
{
// rotate to get the trailing white spaces before the first word
std::rotate( text.begin(), first_ws, next_non_ws ) ;
// rotate to get the leading whitespaces and first word at the end
std::rotate( text.begin(), next_non_ws, text.end() ) ;
}
std::cout << text << '\n' ;
}