Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

reverse order of word in string c#

One Line
public class Solution 
{
    public string ReverseWords(string s) 
      => string.Join(" ",s.Trim().Split(' ').Where(x=>x.Length>0).Reverse());
}

Another Approaches

public class Solution 
{
    public string ReverseWords(string s) 
    {
        var stack = new Stack<string>();
        int i=0 ,j=0;
        while(j <= s.Length)
        {
            if( j == s.Length || s[j] == ' ' )
            {
                var sub = s.Substring(i,j-i);
                if(j-i >= 0 && sub.Length > 0) stack.Push(sub); 
                i=++j;
            }
            else j++;
        }
        return string.Join(" ",stack);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: winforms open multiple forms show one icon in taskabr 
Csharp :: block nulltarge tpl dataflow 
Csharp :: webbrowser control feature_browser_emulation compatible 
Csharp :: c# todatatable nullable 
Csharp :: c# an object on upper level cannot be added to an object 
Csharp :: 10x10 table matrix C# 
Html :: html grundgerüst 
Html :: cdn matter.js 
Html :: bootstrap col center content 
Html :: boostrap row reverse utility 
Html :: leading spaces html 
Html :: rs logo html 
Html :: how to center html heading 
Html :: open vsc with admin rights linux 
Html :: html open link in new tab 
Html :: _blank in html 
Html :: input type file csv only 
Html :: lorem ipsum 
Html :: html email links 
Html :: html change viewport to smartphone size 
Html :: html power 
Html :: import clipboard.js cdn 
Html :: ver pdf html5 
Html :: markdown new page 
Html :: twig lower string 
Html :: align image center of webpage 
Html :: add fav icon to website 
Html :: how to set link as normal text in html 
Html :: html add image from remote source 
Html :: insertar fecha en pagina web 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =