Pages

Saturday, April 21, 2012

Reverse Word In String in C#


Reverse Word In String in C#

Words in a string can be reversed. We have several methods or ways to reverse word in a string.
Using C# you can reverse string:


 Example:
           char [] charAray = "This is Test String".ToCharArray();
            
            StringBuilder sb = new StringBuilder();
            System.Console.WriteLine((new string(charAray)));
            System.Console.WriteLine("Reverse");
            for(int i=charAray.Length-1;i>=0 ;i--)
            {
                if (charAray[i] == ' ')
                {
                   
                    char[] charAray1 = sb.ToString().ToCharArray();
                    Array.Reverse(charAray1);
                    System.Console.Write(new string(charAray1) + " ");
                    sb.Clear();
                }
                else
                {
                    sb.Append(charAray[i]);
                }
            }
            char[] temp = sb.ToString().ToCharArray();
            Array.Reverse(temp);
            System.Console.Write(new string(temp));

No comments:

ShareThis

Welcome

Welcome to Rajesh Prajapati, asp.net blog.
Here you can find some useful code and information about asp.net., c#, VB.net, SQL Server, Web Service, Web Designing etc