Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

Here I split the string into two string. For example, input:

Helloworld

output:

Hello world.

But now I need the output:

dlrow olleH

like image 636
Parthiban Avatar asked Sep 10 '15 07:09

Parthiban


2 Answers

The question is a vague one. If you need put all the words in the string in reversed order, e.g.

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

then you can do

  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);

where ToNameCase() is something like this:

  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();
  }

Edit: if you don't pay attention to case, i.e.

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

you can simplify the solution into just

  String source = "This is a test string";

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

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

Edit 2: If you want to split the text into range chunks with equal lengths (with possible exception of the last chunk), and then reverse them:

  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);
like image 140
Dmitry Bychenko Avatar answered Sep 30 '22 15:09

Dmitry Bychenko


Use Aggregate like this:

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

I just modify your code. It should be look like this now:

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();

Edit: As a better solution I removed redundant code. Check this:

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);
}
like image 25
Salah Akbari Avatar answered Sep 30 '22 15:09

Salah Akbari