使用 while 将字符串拆分为两个单词

Spliting a string into two words using while

string givenstring,outputString="";
int i, j = 0;
Console.WriteLine("Enter the string");
givenstring = Console.ReadLine();
i = (givenstring.Length) / 2;

while (j < i)
{
    outputString += givenstring[j];
    j++;
}

Console.WriteLine(outputString);
outputString = string.Empty;

while (i < givenstring.Length)
{
    outputString += givenstring[i];
    i++;
}
Console.WriteLine(outputString);

这里我把字符串分成了两个字符串。 例如输入:

Helloworld

输出:

Hello world.

但现在我需要输出:

dlrow olleH

像这样使用Aggregate

string reversed = arr.Aggregate((workingSentence, next) => next + " " + workingSentence);
Console.WriteLine(reversed);

我刚刚修改了你的代码。现在应该是这样的:

string givenstring, outputString = "";
int i, j = 0;
string[] arr = new string[2];
Console.WriteLine("Enter the string");
givenstring = Console.ReadLine();
i = (givenstring.Length) / 2;
while (j < i)
{
     outputString += givenstring[j];
     j++;
}
Console.Write("Input -> " + outputString + " ");//hello world
arr[0] = outputString;

outputString = string.Empty;
while (i < givenstring.Length)
{
    outputString += givenstring[i];
    i++;
}
arr[1] = outputString;

Console.WriteLine(outputString);
string reversed = arr.Aggregate((workingSentence, next) => next + " " + workingSentence);
Console.WriteLine("output -> " + reversed);//world hello
Console.WriteLine("output -> " + ReverseString(reversed));//olleh dlrow
Console.ReadLine();

编辑:作为更好的解决方案,我删除了冗余代码。检查这个:

static void Main(string[] args)
{
    var arr = new string[2];
    Console.WriteLine("Enter the string");
    var givenstring = Console.ReadLine();
    if (givenstring != null)
    {
       int i = (givenstring.Length) / 2;
       arr[0] = givenstring.Substring(0 , i);
       arr[1] = givenstring.Substring(i, givenstring.Length - i);
    }
    Console.WriteLine("Input -> " + arr[0] + " " + arr[1]);//hello world
    var reversed = arr.Aggregate((workingSentence, next) => next + " " + workingSentence);
    Console.WriteLine("output -> " + reversed);//world hello
    Console.WriteLine("output -> " + ReverseString(reversed));//olleh dlrow
    Console.ReadLine();
}

static string ReverseString(string str)
{
     char[] chars = str.ToCharArray();
     char[] result = new char[chars.Length];

     for (int i = 0, j = str.Length - 1; i < str.Length; i++, j--)
     {
            result[i] = chars[j];
     }

     return new string(result);
}

这个问题含糊不清。如果您需要将字符串中的所有倒序,例如

"This is a test string" -> "String test a is this"

那么你可以

  String source = "This is a test string";

  String result = String.Join(" ", source
    .Split(' ')
    .Reverse()
    .Select((item, index) => index > 0 ? item.ToLower() : ToNameCase(item)));

  // "String test a is this"
  Console.WriteLine(result);

其中 ToNameCase() 是这样的:

  private static String ToNameCase(String source) {
    if (String.IsNullOrEmpty(source))
      return source;

    StringBuilder sb = new StringBuilder(source.Length);

    sb.Append(Char.ToUpper(source[0]));
    sb.Append(source.Substring(1));

    return sb.ToString();
  }

编辑:如果不注意case,即

"This is a test string" -> "string test a is This"

您可以将解决方案简化为

  String source = "This is a test string";

  String result = String.Join(" ", source
    .Split(' ')
    .Reverse());

  // "string test a is This"
  Console.WriteLine(result);

编辑 2:如果您想将文本拆分为 range 等长 的块(可能的例外是最后一个块),然后反转它们:

  String source = "HelloWorld";
  int range = 2; // we want 2 chunks ("Hello" and "World")

  String result = String.Join(" ", Enumerable
    .Range(0, range)
    .Select(index => index == range - 1 ?
        source.Substring(source.Length / range * index) :
        source.Substring(source.Length / range * index, source.Length / range))
    .Reverse()); // remove ".Reverse()" and you will get "Hello World"

  // "World Hello"
  Console.WriteLine(result);

您可以混合使用 SkipwhileConcatTake 方法:

string s = "Helloworld";

int half = s.Length / 2;

s = new String(s.SkipWhile((ss, i) => i < half)
                .Concat(" ").Concat(s.Take(half)).ToArray());

Console.WriteLine(s);

输出:world Hello

或者您可以简单地连接子字符串:

string givenstring,outputString="";
Console.WriteLine("Enter the string");
givenstring = Console.ReadLine();

int half = givenstring.Length / 2;

givenstring = givenstring.Substring(half , half) + " " + givenstring.Substring(0, half );

(我假设你的字符串只有两个词,因为你没有提供你使用的测试字符串的例子,然后是 Helloworld)...

试试这个

string s = "Helloworld someother string";

var str = s.Split(' ')[0];
int i = str.Length/2;

Console.WriteLine(str.Substring(i) + "  " + str.Substring(0,i));
//or
Console.WriteLine(string.Join(" ",str.Substring(i),str.Substring(0,i)));