C# 整句倒词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15634162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse word of full sentence
提问by
I want to print string in reverse format:
我想以反向格式打印字符串:
Input: My name is Archit Patel
输入: My name is Archit Patel
Output: Patel Archit is name My
.
输出:Patel Archit is name My
。
I've tied the following but it displays as letaP tihcrA si eman ym
.
我已经绑定了以下内容,但它显示为letaP tihcrA si eman ym
.
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
回答by Guffa
You would need to split the string into words and the reverse those instead of reversing the characters:
您需要将字符串拆分为单词并反转它们而不是反转字符:
text = String.Join(" ", text.Split(' ').Reverse())
In framework 3.5:
在框架 3.5 中:
text = String.Join(" ", text.Split(' ').Reverse().ToArray())
In framework 2.0:
在框架 2.0 中:
string[] words = text.Split(' ');
Array.Reverse(words);
text = String.Join(" ", words);
回答by RobIII
"please send me the code of this program."
“请把这个程序的代码发给我。”
Okay ...
好的 ...
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string text = "My name is Archit Patel";
Console.WriteLine(string.Join(" ", text.Split(' ').Reverse()));
}
}
Now: what have you learned?
现在:你学到了什么?
Also, as Guffa points out, for versions below .Net 4.0 you'll need to add .ToArray()
since string.Join doesn't have the correct overloadin those versions.
此外,正如Guffa 指出的那样,对于低于 .Net 4.0 的版本,您需要添加,.ToArray()
因为 string.Join在这些版本中没有正确的重载。
回答by Crowlix
Use the split method to put it in an array
使用split方法将其放入数组中
string[] words = s.Split(' ');
Then reverse the array with array.reverse
然后用 array.reverse 反转数组
words = Array.Reverse(words);
Now you can print it with a for-each loop and add spaces
现在您可以使用 for-each 循环打印它并添加空格
Hope this helps
希望这可以帮助
回答by Nikola Davidovic
You could try:
你可以试试:
string[] words = "My name is Archit Patel".Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<string> reverseWords = words.Reverse();
string reverseSentence = String.Join(" ", reverseWords);
回答by waqar habib
this.lblStringReverse.Text = Reverse(this.txtString.Text);
this.lblStringReverse.Text = Reverse(this.txtString.Text);
private int NoOfWhiteSpaces(string s)
{
char[] sArray = s.ToArray<char>();
int count = 0;
for (int i = 0; i < (sArray.Length - 1); i++)
{
if (sArray[i] == ' ') count++;
}
return count;
}
private string Reverse(string s)
{
char[] sArray = s.ToArray<char>();
int startIndex = 0, lastIndex = 0;
string[] stringArray = new string[NoOfWhiteSpaces(s) + 1];
int stringIndex = 0, whiteSpaceCount = 0;
for (int x = 0; x < sArray.Length; x++)
{
if (sArray[x] == ' ' || whiteSpaceCount == NoOfWhiteSpaces(s))
{
if (whiteSpaceCount == NoOfWhiteSpaces(s))
{
lastIndex = sArray.Length ;
}
else
{
lastIndex = x + 1;
}
whiteSpaceCount++;
char[] sWordArray = new char[lastIndex - startIndex];
int j = 0;
for (int i = startIndex; i < lastIndex; i++)
{
sWordArray[j] = sArray[i];
j++;
}
stringArray[stringIndex] = new string(sWordArray);
stringIndex++;
startIndex = x+1;
}
}
string result = "";
for (int y = stringArray.Length - 1; y > -1; y--)
{
if (result == "")
{
result = stringArray[y];
}
else
{
result = result + ' ' + stringArray[y];
}
}
return result;
}
回答by Zhuhulo
public static string reversewordsInsentence(string sentence)
{
string output = string.Empty;
string word = string.Empty;
foreach(char c in sentence)
{
if (c == ' ')
{
output = word + ' ' + output;
word = string.Empty;
}
else
{
word = word + c;
}
}
output = word + ' ' + output;
return output;
}
回答by nihnih
If you want non-linq solution:
如果你想要非 linq 解决方案:
static string ReverseIntact(char[] input)
{
//char[] input = "dog world car life".ToCharArray();
for (int i = 0; i < input.Length / 2; i++)
{//reverse the expression
char tmp = input[i];
input[i] = input[input.Length - i - 1];
input[input.Length - i - 1] = tmp;
}
for (int j = 0, start = 0, end = 0; j <= input.Length; j++)
{
if (j == input.Length || input[j] == ' ')
{
end = j - 1;
for (; start < end; )
{
char tmp = input[start];
input[start] = input[end];
input[end] = tmp;
start++;
end--;
}
start = j + 1;
}
}
return new string(input);
}
回答by adSad
This should do your job! OUTPUT: "Patel Archit is name My".
这应该可以完成您的工作!输出:“Patel Archit 是我的名字”。
static void Main(string[] args)
{
string sentence = "My name is Archit Patel";
StringBuilder sb = new StringBuilder();
string[] split = sentence.Split(' ');
for (int i = split.Length - 1; i > -1; i--)
{
sb.Append(split[i]);
sb.Append(" ");
}
Console.WriteLine(sb);
Console.ReadLine();
}
回答by Bharath C N
namespace Reverse_the_string
{
class Program
{
static void Main(string[] args)
{
// string text = "my name is bharath";
string text = Console.ReadLine();
string[] words = text.Split(' ');
int k = words.Length - 1;
for (int i = k;i >= 0;i--)
{
Console.Write(words[i] + " " );
}
Console.ReadLine();
}
}
}
回答by Ronen Festinger
This is my solution to an interview question if you need to do it in place. I use one more character for the swap. I assume space is only used as a separator.
如果您需要就地进行,这是我对面试问题的解决方案。我再使用一个字符进行交换。我假设空格仅用作分隔符。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("abcd efg hijk");
Reverse(sb, 0 , sb.Length - 1);
Console.WriteLine(sb);
ReverseWords(sb);
Console.WriteLine(sb);
ReverseWordOrder(sb);
Console.WriteLine(sb);
}
static void Reverse(StringBuilder sb, int startIndex, int endIndex)
{
for(int i = startIndex; i <= (endIndex - startIndex) / 2 + startIndex; i++)
{
Swap(sb,i, endIndex + startIndex - i);
}
}
private static void Swap(StringBuilder sb, int index1, int index2)
{
char temp = sb[index1];
sb[index1] = sb[index2];
sb[index2] = temp;
}
static void ReverseWords(StringBuilder sb)
{
int startIndex = 0;
for (int i = 0; i <= sb.Length; i++)
{
if (i == sb.Length || sb[i] == ' ')
{
Reverse(sb, startIndex, i - 1);
startIndex = i + 1;
}
}
}
static void ReverseWordOrder(StringBuilder sb)
{
Reverse(sb, 0, sb.Length - 1);
ReverseWords(sb);
}
}
}