Move the first word in a line of text to the end.

Hi,

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

Any help will be greatly appreciated!

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.
Thank you for the quick reply, Chervil.
will look into it
I missed out append() (you can also use the += operator) which could be useful too.
still having trouble :(
post the code you have so far.
I can post an ugly code that I've quickly done.
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
36
37
38
39
40
41
#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];
        }
        else break;
    }
    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.
Using power of C++11: http://ideone.com/odLZh1
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
1
2
3
4
5
6
7
8
9
10
11
    size_t pos = sentence.find(' ');
    if (pos != string::npos)
    {
        string word = sentence.substr(0, pos);
        pos = sentence.find_first_not_of(' ', pos);
        if (pos != string::npos)
        {        
            sentence.erase(0, pos);
            sentence += ' ' + word;
        }
    }

But i also considered a shorter version using stringstream.
Demo: http://ideone.com/3P4KWT
Here is my version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main ()
{
    
    std::string str;  // String Vars
    std::string str1;
    std::cout << "Enter a sentence: \n"; // getting input 
    getline(std::cin, str, '\n');
    str1 = str; // copying string
    int i = 0; // Looping for first space
    while (str[i] != ' ') {
        i++; 
    }
    str.erase (str.begin(), str.end()-str.length()+i);     // truncating string
    std::cout << str << " " << str1.substr(0, i) << '\n';  // printing out string
    return 0;
}
Chervil wrote:
But i also considered a shorter version using stringstream.
Demo: http://ideone.com/3P4KWT
/facepalm
Somehow I did not though about that you do not have to reconstruct whole string.
@MiiNiPaa I learned some things too - I'm not quite so familiar with what <algorithm> has to offer.
<algorithm> has std::rotate() to offer. http://en.cppreference.com/w/cpp/algorithm/rotate
It can preserve the original spacing (including tabs).

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 <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
      const auto first_ws = std::find_if( text.begin(), text.end(), []( char c ) { return std::isspace(c) ; } ) ;
      const auto 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' ;
}

http://coliru.stacked-crooked.com/a/f0e6fe3e06f34e67
Topic archived. No new replies allowed.