反向打印句子 [英] To print the sentence reverse

查看:66
本文介绍了反向打印句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以相反的方式打印句子?

不是字符串反向.它的句子反向.

How do i print the sentence in reverse manner?

Not string reverse.Its sentense reverse.

推荐答案

将句子拆分成单个单词,然后反向构建一个新句子:
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());


卢阿:
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


还有一个版本:):
And one more version :) :
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:

Reverser.DoTheJob("Some string in here");


和输出


And the output

here in string Some


这篇关于反向打印句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆