Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to print string in reverse format, my input is "My name is Ankush Jassal" & i need output in this way "Jassal Ankush is name My".
I've try but it display "lassal hsukna si eman ym". I need the above format how will i do it. Could you please send me the code of this program.
Posted
Comments
Member 9762654 26-Mar-13 5:22am    
have you solved the problem?

Or by using System.Linq;

C#
string str = "My name is Ankush Jassal";
string[] strsplit = str.Split(' ');
string revstr = string.Join(" ", strsplit.Reverse().ToArray());
Console.WriteLine(revstr);
 
Share this answer
 
Comments
sudeshchandram 26-Mar-13 10:56am    
This is a very good answer !
Per Söderlund 27-Mar-13 5:33am    
Thanks
Hi may be it will help u.
C#
string str = "My name is Ankush Jassal";
            string[] sub = str.Split(' ');

            for (int i = sub.Length-1; i >=0 ; i--)
            {
                Console.Write(sub[i] +" ");
            }


jmd
:-)
 
Share this answer
 
Comments
[no name] 26-Mar-13 6:16am    
This one the Best Solution.............
giri001 26-Mar-13 9:10am    
Thanks Rakesh..:-)
[no name] 28-Mar-13 7:11am    
your comments are Showing ............How much Mature r you? By the way you are not responsible to guide other......you have to look your own solution.
C#
public static string Reverse(string s)
       {
           string[] strArray = s.Split(new char[]{' '});
           Array.Reverse(strArray);
           return string.Join(" ", strArray);

       }
 
Share this answer
 
v2
Comments
Per Söderlund 26-Mar-13 4:19am    
This will also reverse the words, it´s not what was asked.
Here we go!

C#
string str = "My name is Ankush Jassal";
            string[] arr = str.Split(' ');
            int ix = 0;
            int jx = arr.Length - 1;

            while (ix < jx)
            {
                string temp = arr[ix];
                arr[ix] = arr[jx];
                arr[jx] = temp;
                ix++;
                jx--;
            }

            string reversedStr = string.Join(" ", arr);
            Console.WriteLine(reversedStr);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900