2) Reverse a string
Write a function which accepts a string and returns a string with the words reversed. E,g “This is an example string” becomes “string example an is This”.many thanks
Write a function which accepts a string and returns a string with the words reversed. E,g “This is an example string” becomes “string example an is This”.many thanks
using System.Text;namespace RevString
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StringBuilder org = new StringBuilder("This is an example string"); int length = org.Length; for(int i=0; i < length/2; i++)
{
char temp;
temp = org[i];
org[i]= org[length-i-1];
org[length-i-1]= temp;
} Console.WriteLine(org);
}
}
}
thank you all.
string[] words = sen.Split(new char[]{' '});
for (int i=words.Length-1; i>=0; i--)
{
Console.Write(words[i] + " ");
}Result:
string example an is This