Reversing words in a string- Method 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReverseString
{
class Program
{
static void Main(string[] args)
{
string test = "Hi How are you";
int len = test.Length;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
bool spaceOccurred = false;

char[] charArray = test.ToCharArray();
char[] finalArray = test.ToCharArray();
char[] reverseArray = test.ToArray();

for (i = len-1; i >= 0; i-- )
{
finalArray[j] = charArray[i];
j++;
}
string finalString = new string(finalArray);
Console.WriteLine(finalString);

for (i = 0; i < len; i++)
{
if (finalArray[i] == ' ')
{
if (!spaceOccurred)
{
for (k = i-1; k >= 0; k--)
{
if (finalArray[k] != ' ')
{
reverseArray[l] = finalArray[k];
l++;
}
else
{
break;
}
}
reverseArray[l] = ' ';
l++;
spaceOccurred = true;
}
else
{
reverseArray[l] = ' ';
l++;
}
}
else
{
spaceOccurred = false;
}
}

for (i = len - 1; finalArray[i] != ' '; i--)
{
reverseArray[l] = finalArray[i];
l++;
}

Console.WriteLine(reverseArray);
Console.ReadLine();
}
}
}

Comments

Popular posts from this blog

Network Security: LAN manager authentication level

Cisco AnyConnect Secure Mobility Client - VPN service not available. The VPN agent service is not responding