How to Reverse only even portion of Strings

Posted by Ras90it under C# on 6/18/2012 | Points: 10 | Views : 757 | Status : [Member] | Replies : 4
How to Reverse only even portion of Strings

Input : This Is Very Nice Place

Output : This sI Very eciN Place

by

Raseeth.A



Responses

Posted by: Ajay.Kalol on: 6/19/2012 [Member] Starter | Points: 25

Up
0
Down
use string.split(" ") function that will return array by spliting string as per your condition.

Now use for loop on array.
put if condition for even no.

if condition is true then write reverse string logic.


I have told you flow Now Make program from it if there is any problem i will explain you.
But it is not good thing to copy/Paste any Program.

This is Very simple Logic program, This may asked you in practicals when you go for interview.
so try it.

Ajay
ajaypatelfromsanthal.blogspot.in

Ras90it, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/19/2012 [Member] Silver | Points: 25

Up
0
Down
@Ajay.Kalol
Exactly as you said..
@Raseeth.A
Let us know,What have you tried?

Ras90it, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Swapna901 on: 6/20/2012 [Member] Starter | Points: 25

Up
0
Down
private string RevesreString(string String)
{
string MainString = string.Empty;
MainString = String;
string ReverseString = string.Empty;
string[] split = MainString.Split(' ');
for (int i = 1; i <= split.Length; i++)
{
if (i % 2 == 0)
{

string str = split[i - 1];
for (int j = str.Length - 1; j >= 0; j--)
{
ReverseString = ReverseString + str[j] ;
}

ReverseString = ReverseString + ' ';
}
else
{
ReverseString=ReverseString + split[i-1] + ' ';
}

}

Response.Write(ReverseString);
return ReverseString;
}

Ras90it, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ganeshd on: 6/20/2012 [Member] Starter | Points: 25

Up
0
Down
static void Main()
{
string text = "This Is Very Nice Place";
Console.WriteLine("Input:" + text);
string[] array = text.Split(' ');

StringBuilder res = new StringBuilder();
string reverse = string.Empty;
for (int i = 0; i < array.Count(); i++)
{
reverse = string.Empty;
if (i % 2 == 0)
{
if (i == 0)
res.Append(array[i]);
else
res.Append(' ' + array[i]);
}
else
{
char[] charray = array[i].ToCharArray();
for (int j = charray.Count(); j > 0; j--)
{
reverse += charray[j - 1];
}
res.Append(' ' + reverse);
}
}
Console.WriteLine("Output:" + res.ToString());

Console.Read();
}

Ras90it, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response