Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
How do i print the sentence in reverse manner?

Not string reverse.Its sentense reverse.
Posted
Updated 7-Apr-11 23:27pm
v2
Comments
musefan 8-Apr-11 4:53am    
Please explain more. What language are you using? and what do you mean by reverse sentence? do you mean to write all or the words in reverse order?
Nuri Ismail 8-Apr-11 4:53am    
Please, tag your question in order to clarify which programming language you use.
johannesnestler 8-Apr-11 5:06am    
Just wondering. How can this be useful? Maybe homework, but I think the teacher should better create some "realistic" examples.
Sandeep Mewara 8-Apr-11 6:45am    
No effort.
Sergey Alexandrovich Kryukov 9-Apr-11 0:05am    
No effort, bad formulation. Why are you people even bothered to Answer.
--SA

Break the sentence into individual words, then build a new sentence in reverse:
string sentence = "The quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
StringBuilder sb = new StringBuilder(sentence.Length);
string separator = "";
for (int i = words.Length; i > 0; i--)
    {
    sb.Append(separator);
    sb.Append(words[i - 1]);
    separator = " ";
    }
Console.WriteLine(sb.ToString());
 
Share this answer
 
Lua:
Lua
s = "alpha beta gamma #wer5"
t = {}
for w in string.gmatch(s, "([^%s]+)") do
  table.insert(t,w)
end
for i = 1,#t do
  io.write(t[#t-i+1] .. " ")
end
 
Share this answer
 
v2
Comments
Wendelius 8-Apr-11 13:35pm    
Nice :)
And one more version :) :
C#
public static class Reverser {
    public class Word {
        public int Index { get; set; }
        public string TheWord { get; set; }
    }
    public static string DoTheJob(string sentence) {
        int counter = 0;
        string result;

        var query = from word in sentence.Split(' ')
                     select new Word {
                         Index = counter++,
                         TheWord = word
                     };
        result = string.Join(" ", query.OrderByDescending(i => i.Index).Select(i => i.TheWord).ToArray<object>());

        return result;
    }
}

The usage:
C#
Reverser.DoTheJob("Some string in here");

And the output
here in string Some
 
Share this answer
 
Comments
RaviRanjanKr 8-Apr-11 13:14pm    
Good One! My 5
Wendelius 8-Apr-11 13:35pm    
Thanks, too much spare time I guess :)
CPallini 8-Apr-11 14:31pm    
5. Reversing is apparently fascinating.
Wendelius 8-Apr-11 14:35pm    
Thanks, reversing is definitely one of the most rewarding tasks in life, has to be :)
Here is my guess. It is in C#

to reverse a sentence...

string result = "";
string sentence = "This is a sentence";

string[] words = sentence.Split(' ');
for(int i = words.Count - 1; i >= 0; i--)
    result += words[i] + " ";

result = result.Trim();
//result is now "sentence a is This"
 
Share this answer
 
Two C# answers. :)

Here is my C++ version:
C++
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    // Our test string
    std::string str = "One Two Three Four Five";
    
    // Extract words from sentence
    std::vector<std::string> words;
    std::istringstream iss(str);
    for(std::string word; iss >> word; words.push_back(word));
    
    // Fill the new string with extracted words in reverse order
    std::string rwstr;
    for(int i = words.size() - 1; i >= 0; rwstr += words[i--] + " ");
    
    // Check the results
    std::cout << "\n Original string: " << str;  // Prints: One Two Three Four Five
    std::cout << "\n Word order reversed: " << rwstr; // Prints: Five Four Three Two One
    return 0;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900