.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

string reverse

Posted By: Rama Krishna     Posted Date: September 17, 2010    Points:2   Category :C#
static void Main(string[] args)
{
string str = "Manhatton Associates";
string rev = "";

for (int i =str.Length-1; i >= 0; i--)
{
rev+= str[i];
}

Console.WriteLine("Original: {0}\nReversed: {1}", str, rev);
Console.ReadLine();
}

the above code gives the output as :setaicossA nottahnaM
but i want :nottahnaM setaicossA

could any modify my code to get abouve output without using built in functions

Responses
Author: Santosh             
Posted Date: September 17, 2010     Points: 5   

hi rama krishna

after getting this output,u can swap this two words.

like: declare a array of string and only a string

use split fuction to split the word using the separator "space" and assign to the array.

then your finalstring/output will be

finalstring = array[1] & array[0]



Regards
Santosh


Author: Asha.MP             
Posted Date: September 17, 2010     Points: 5   

Hi..
Check this code it will be helpful to you,
this will work for all cases, you can also take user input.

public static void Main()
{
//String to reverse
string StrToRev = "Asha MP Softwere Engineer";
string str = "";
int NoOfSpaces = 0;
int i;
string[] ArrToStore = new string[6];
for (i = 0; i <= StrToRev.Length - 1; i++)
{
str += StrToRev[i];
//Comparing char with space
if (StrToRev[i] == ' ')
{
ArrToStore[NoOfSpaces] = RevStr(str, str.Length - 2);
str = "";
NoOfSpaces += 1;
}

}
//To reverse and add last word
ArrToStore[NoOfSpaces] = RevStr(str, str.Length - 1);

string revercedStr = "";

for (i = 0; i <= NoOfSpaces; i++)
{
//Appending space with string
revercedStr = revercedStr + ArrToStore[i] + " ";
}

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

public static string RevStr(string SplitString, int SpacePos)
{
string revString = "";
while (SpacePos >= 0)
{
revString += SplitString[SpacePos];
SpacePos -= 1;
}

return revString;
}


Thanks and Regards,
Asha.mp
Author: Deco             
Posted Date: September 18, 2010     Points: 5   

please try my intelligent code

string str = "Manhatton Associates";
string rev = "";
string[] splitstr = str.Split(' ');
for (int i = 0; i < splitstr.Length; i++)
{
string getSplittedValue = splitstr[i];

for (int j = getSplittedValue.Length - 1; j >= 0; j--)
{
rev += getSplittedValue[j];
}
rev = rev + " ";
}


Regards,
Deco
Author: Suthish Nair             
Posted Date: September 20, 2010     Points: 5   

another way.. :-)

using System.Collections;


string[] str = "Manhatton Associates".ToString().Split(' ');
IEnumerable<string> items = str;
char[] charArray;
foreach (var item in items)
{
charArray = item.ToCharArray();
Array.Reverse(charArray);
new string(charArray);
for (int i = 0; i < charArray.Length; i++)
{
Console.Write(charArray[i]);
}
Console.Write(" ");
}
Console.ReadLine();



suthish nair


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend