Given a string, how do we reverse the order or words in the string. e.g. Input: Tomorrow is Sunday”. Output: Sunday is Tomorrow.
I am using Visual Studio and open Console Application:
Add one class with named "ReverseString". Flowing code snips will help you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticTest
{
class ReverseString
{
public static String reverse(string authors)
{
string[] authorsList = authors.Split(' ');
String result = "";
for (int i = authorsList.Length - 1; i >= 0; i--)
{
result = result + authorsList[i] + " ";
}
return result;
}
public static String reverseWord(string authors)
{
string[] authorsList = authors.Split(' ');
String result = "";
for (int i = authorsList.Length - 1; i >= 0; i--)
{
string resultSub = string.Empty;
string subString = authorsList[i];
for (int j= subString.Length-1; j>=0;j--)
{
resultSub = resultSub + subString[j];
}
result = result + resultSub+ " ";
}
return result;
}
public static String reverseWordOnly(string authors)
{
string[] authorsList = authors.Split(' ');
String result = "";
for (int i = 0; i <= authorsList.Length - 1; i++)
{
string resultSub = string.Empty;
string subString = authorsList[i];
for (int j = subString.Length - 1; j >= 0; j--)
{
resultSub = resultSub + subString[j];
}
result = result + resultSub + " ";
}
return result;
}
}
}
---------------------------------------------------------------------------------------------------------
Output:
No comments:
Post a Comment