0

I'm having difficulty creating a function that reverse the order of the sentence around. I've read many functions on how to recursively reverse the letters around and I have successfully done so, but I do not want to reverse the letters in the words. I want to reverse the placement of the words in the sentence.

Example would be:

This is a sentence.

sentence. a is This

This is my code so far. How do I go from reversing order of letters of the entire sentence to placement order of words in a sentence?

The output of the current code would provide: !dlroW olleH

void reverse(const std::string str)
{
    int length = str.size();
    if(length > 0)
    {
        reverse(str.substr(0,length-1));
        std::cout << str[0];

    }
}

Edit: Additional question. If this was a char array would the logic be different?

Touchpad
  • 29
  • 1
  • 10
  • 1
    You need to 1) split your input sentence on words, 2) reverse the list of words (i.e. do the same thing as for characters). On which of these steps you faced a problem? – Ilya Feb 08 '17 at 06:01
  • I am having trouble with splitting the input sentence into words. – Touchpad Feb 08 '17 at 06:18
  • 1
    http://stackoverflow.com/questions/236129/split-a-string-in-c – macroland Feb 08 '17 at 06:18

5 Answers5

5

Simplify your logic by using a std::istringstream and a helper function. The program below works for me.

#include <sstream>
#include <iostream>

void reverse(std::istringstream& stream)
{
   std::string word;
   if ( stream >> word )
   {
      reverse(stream);
      std::cout << word << " ";
   }
}

void reverse(const std::string str)
{
   std::istringstream stream(str);
   reverse(stream);
   std::cout << std::endl;
}

int main(int argc, char** argv)
{
   reverse(argv[1]);
   return 0;
}
Andreas
  • 5,393
  • 9
  • 44
  • 53
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1
std::vector<std::string> splitString(const std::string &s, char delim) {
    std::stringstream ss(s);
    std::string item;
    std::vector<std::string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}
void reverseString(const std::string& string) {

    std::vector<std::string> words = splitString(string, ' ');
    auto end = words.rend();
    for (auto it = words.rbegin(); it <= end; it++) {
        std::cout << *it << std::endl;
    }
}
reverseString("This is a sentence.");
Khurram Shehzad
  • 261
  • 3
  • 12
1
// Pass string which comes after space
// reverse("This is a sentence.") 
// reverse("is a sentence.") 
// reverse("a sentence.") 
// reverse("sentence.") 
// will not find space 
// start print only word in that function

void reverse(const std::string str)
{
    int pos = str.find_first_of(" ");
    if (pos == string::npos) // exit condition
    {
        string str1 = str.substr(0, pos);
        cout << str1.c_str() << " " ;
        return;
    }

    reverse(str.substr(pos+1));
    cout << str.substr(0, pos).c_str() << " ";
}

Simple to understand:

void reverse(const std::string str)
{
    int pos = str.find_first_of(" ");
    if (pos != string::npos) // exit condition
    {
        reverse(str.substr(pos + 1));
    }
    cout << str.substr(0, pos).c_str() << " ";
}
Swapnil
  • 1,424
  • 2
  • 19
  • 30
1

You can split input and print them in inverse order Or if you want to use recursive structure just move the cout after calling a function like this:

void reverse(const std::string str)
{
    std::stringstream ss(str);
    std::string firstWord, rest;
    if(ss >> firstWord)
    {
        getline(ss , rest);
        reverse(rest);
        std::cout << firstWord << " ";
    }
}
Sam Mokari
  • 461
  • 3
  • 11
  • Question is the if statement allowing firstWord output? Would you mind explaining? Coming from java this part seems interesting. – Touchpad Feb 08 '17 at 06:45
  • ss >> firstWord read one string from a stream. If the stream is empty, the operator returns false. Take a look at c++ streams. – Sam Mokari Feb 08 '17 at 22:27
0

I am not a C++ programmer, but I would create another array (tempWord[ ]) to store individual word.

  1. Scan each word and store them into tempWord array. In your case, the words are separated by space, so:

    a.get the index of the next space,

    b substring to the index of the next space and

    c. you should get {"This", "is", "a", "sentence."}

  2. Add them up again reversely:

    a. loop index i from "tempWord.length -1" to "0"

    b. new String = tempWord[i]+" ";

  3. print out result.

Di Wang
  • 471
  • 1
  • 8
  • 22