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
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);
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With