Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,161 questions

56,039 answers

573 users

How to sort the words in a string with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class SortTheWordsInAString_CSharp
{
    public static string SortTheWordsInAString(string s) {
        List<string> arr = new List<string>();

        string[] tokens = s.Split(' ');
        arr.AddRange(tokens);

        arr.Sort();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (string str in arr) {
            sb.Append(str).Append(" ");
        }

        if (sb.Length > 0) {
            sb.Length--; // Remove the last space
        }

        return sb.ToString();
    }

    public static void Main(string[] args)
    {
        string s = "php c java c++ python c#";

        s = SortTheWordsInAString(s);

        Console.WriteLine(s);
    }
}


    
/*
run:
    
c c# c++ java php python
    
*/

 



answered Jul 29, 2024 by avibootz
...