Reverse Sentence using c#
using System;
namespace ReverseString_Sentence
{
class Program
{
static void Main(string[] args)
{
string str = "Hello World!";
Console.WriteLine(str);
string finalStr = "";
string midStr = "";
str = " " + str;
for (int i = str.Length - 1; i >= 0; i--)
{
if (str[i].ToString() != " ")
{
midStr = midStr + str[i];
}
else
{
for (int k = midStr.Length - 1; k >= 0; k--)
{
finalStr = finalStr + midStr[k];
}
finalStr = finalStr + " ";
midStr = "";
}
}
finalStr = finalStr + " " + midStr;
finalStr = finalStr.Trim();
Console.WriteLine(finalStr);
Console.Read();
}
}
}
namespace ReverseString_Sentence
{
class Program
{
static void Main(string[] args)
{
string str = "Hello World!";
Console.WriteLine(str);
string finalStr = "";
string midStr = "";
str = " " + str;
for (int i = str.Length - 1; i >= 0; i--)
{
if (str[i].ToString() != " ")
{
midStr = midStr + str[i];
}
else
{
for (int k = midStr.Length - 1; k >= 0; k--)
{
finalStr = finalStr + midStr[k];
}
finalStr = finalStr + " ";
midStr = "";
}
}
finalStr = finalStr + " " + midStr;
finalStr = finalStr.Trim();
Console.WriteLine(finalStr);
Console.Read();
}
}
}
Comments
Post a Comment