Reversing Words - Method 2
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 = 0;
char[] charArray = test.ToCharArray();
char[] finalArray = test.ToCharArray();
int i = 0;
int j = 0;
int k = 0;
bool spaceOccurred = false;
foreach (char ch in charArray)
{
i++;
}
len = i;
for (i = len-1; i >= 0; i--)
{
if (charArray[i] == ' ' || i == 0)
{
if (!spaceOccurred || i == 0)
{
if (i == 0)
{
i = -1;
}
for (k = i+1; k != len && charArray[k] != ' '; k++)
{
finalArray[j] = charArray[k];
j++;
}
if (i != -1)
{
finalArray[j] = ' ';
j++;
}
spaceOccurred = true;
}
else
{
finalArray[j] = ' ';
j++;
}
}
else
{
spaceOccurred = false;
}
}
Console.WriteLine(finalArray);
Console.ReadLine();
}
}
}
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 = 0;
char[] charArray = test.ToCharArray();
char[] finalArray = test.ToCharArray();
int i = 0;
int j = 0;
int k = 0;
bool spaceOccurred = false;
foreach (char ch in charArray)
{
i++;
}
len = i;
for (i = len-1; i >= 0; i--)
{
if (charArray[i] == ' ' || i == 0)
{
if (!spaceOccurred || i == 0)
{
if (i == 0)
{
i = -1;
}
for (k = i+1; k != len && charArray[k] != ' '; k++)
{
finalArray[j] = charArray[k];
j++;
}
if (i != -1)
{
finalArray[j] = ' ';
j++;
}
spaceOccurred = true;
}
else
{
finalArray[j] = ' ';
j++;
}
}
else
{
spaceOccurred = false;
}
}
Console.WriteLine(finalArray);
Console.ReadLine();
}
}
}
Comments
Post a Comment