Reverse String


public static String Reverse(string input)
{
var length = input.Length;
var buffer = new char[length];
for (var i = 0; i < input.Length; i++)
{
buffer[i] = input[(length – i) – 1];
}
return new String(buffer);
}

private string ReverseWordsLogic2(string str)
{
int count = 0;
char[] charArray = str.ToCharArray();
int len = str.Length – 1;
for (int i = 0; i <= len; i++)
{
if (charArray[i].ToString() == ” ” || charArray[i] == ‘ ‘)
count += 2;
}

string[] words = new string[count + 1];
string word = “”;
int icount = 0;
count = 0;
for (int i = 0; i <= len; i++)
{
if (charArray[i] != ‘ ‘)
{
word += charArray[i];
}
else
{
words[icount] = word;
words[icount + 1] = ” “;
icount += 2;
word = “”;
}
}
words[icount] = word;
word = “”;

int wordlen = words.Length – 1;

for (int i = 0; i <= wordlen; i++)
words[i] = words[wordlen – i];

for (int i = 0; i <= wordlen; i++)
word += word[i].ToString();
return word;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: