Sort words of string in ascending order

Here given code implementation process.

import java.util.Arrays;
/*
    Java Program for
    Sort words of string in ascending order
*/
public class Sorting
{
	public void sortWord(String text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		String[] words = text.split(" ");
		// Sort word value
		Arrays.sort(words);
		String output = "";
		// Collect sort word
		for (int i = 0; i < words.length; ++i)
		{
			if (i != 0)
			{
				output += " ";
			}
			output += words[i];
		}
		// Display given text
		System.out.println(" Given Text  : " + text);
		// Display result
		System.out.println(" Sorted Word : " + output);
	}
	public static void main(String[] args)
	{
		Sorting task = new Sorting();
		String text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
		task.sortWord(text);
	}
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
// Include header file
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
/*
    C++ Program for
    Sort words of string in ascending order
*/
class Sorting
{
    public: void sortWord(char text[])
    {
        int n = strlen(text);
        if (n == 0)
        {
            return;
        }
        // Spit text by space
        char *words = strtok(text, " ");

        string input = "";
        string output = "";
        bool first = true;
        vector <string> record;

        while (words != NULL)
        {
            if(!first)
            {
                input += " ";
            }
            input += words;
            record.push_back(words);
            words = strtok (NULL, " ");
            first = false;
        }
        // Sort word value
        sort(record.begin(), record.end());

        // Collect sort word
        for (int i = 0; i < record.size(); ++i)
        {
            if (i != 0)
            {
                output += " ";
            }
            output += record[i];
        }
        // Display given text
        cout << " Given Text  : " << input << endl;
        // Display result
        cout << " Sorted Word : " << output << endl;
    }
};
int main()
{
    Sorting *task = new Sorting();
    char text[] = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
    task->sortWord(text);
    return 0;
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
package main
import (
    "fmt"
    "sort"
    "strings"
)
/*
    Go Program for
    Sort words of string in ascending order
*/
type Sorting struct {}
func getSorting() * Sorting {
	var me *Sorting = &Sorting {}
	return me
}
func(this Sorting) sortWord(text string) {
	var n int = len(text)
	if n == 0 {
		return
	}
	// Spit text by space
	var words = strings.Split(text, " ")
	// Sort word value
	sort.Strings(words)
	var output string = ""
	// Collect sort word
	for i := 0 ; i < len(words) ; i++ {
		if i != 0 {
			output += " "
		}
		output += words[i]
	}
	// Display given text
	fmt.Println(" Given Text  : ", text)
	// Display result
	fmt.Println(" Sorted Word : ", output)
}
func main() {
	var task * Sorting = getSorting()
	var text string = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries"
	task.sortWord(text)
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
// Include namespace system
using System;
/*
    Csharp Program for
    Sort words of string in ascending order
*/
public class Sorting
{
	public void sortWord(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		String[] words = text.Split(" ");
		// Sort word value
		Array.Sort(words);
		String output = "";
		// Collect sort word
		for (int i = 0; i < words.Length; ++i)
		{
			if (i != 0)
			{
				output += " ";
			}
			output += words[i];
		}
		// Display given text
		Console.WriteLine(" Given Text  : " + text);
		// Display result
		Console.WriteLine(" Sorted Word : " + output);
	}
	public static void Main(String[] args)
	{
		Sorting task = new Sorting();
		String text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
		task.sortWord(text);
	}
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
<?php
/*
    Php Program for
    Sort words of string in ascending order
*/
class Sorting
{
	public	function sortWord($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		// Spit text by space
		$words = explode(" ", $text);
		// Sort word value
		sort($words);
		$output = "";
		// Collect sort word
		for ($i = 0; $i < count($words); ++$i)
		{
			if ($i != 0)
			{
				$output .= " ";
			}
			$output .= $words[$i];
		}
		// Display given text
		echo(" Given Text  : ".$text.
			"\n");
		// Display result
		echo(" Sorted Word : ".$output.
			"\n");
	}
}

function main()
{
	$task = new Sorting();
	$text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
	$task->sortWord($text);
}
main();

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
/*
    Node JS Program for
    Sort words of string in ascending order
*/
class Sorting
{
	sortWord(text)
	{
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		var words = text.split(" ");
		// Sort word value
		words.sort();
		var output = "";
		// Collect sort word
		for (var i = 0; i < words.length; ++i)
		{
			if (i != 0)
			{
				output += " ";
			}
			output += words[i];
		}
		// Display given text
		console.log(" Given Text  : " + text);
		// Display result
		console.log(" Sorted Word : " + output);
	}
}

function main()
{
	var task = new Sorting();
	var text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
	task.sortWord(text);
}
main();

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
#    Python 3 Program for
#    Sort words of string in ascending order
class Sorting :
	def sortWord(self, text) :
		n = len(text)
		if (n == 0) :
			return
		
		#  Spit text by space
		words = text.split(" ")
		#  Sort word value
		words.sort()
		output = ""
		i = 0
		#  Collect sort word
		while (i < len(words)) :
			if (i != 0) :
				output += " "
			
			output += words[i]
			i += 1
		
		#  Display given text
		print(" Given Text  : ", text)
		#  Display result
		print(" Sorted Word : ", output)
	

def main() :
	task = Sorting()
	text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries"
	task.sortWord(text)

if __name__ == "__main__": main()

Output

 Given Text  :  Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word :  Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
#    Ruby Program for
#    Sort words of string in ascending order
class Sorting 
	def sortWord(text) 
		n = text.length
		if (n == 0) 
			return
		end

		#  Spit text by space
		words = text.split(" ")
		#  Sort word value
		words = words.sort
		output = ""
		i = 0
		#  Collect sort word
		while (i < words.length) 
			if (i != 0) 
				output += " "
			end

			output += words[i]
			i += 1
		end

		#  Display given text
		print(" Given Text  : ", text, "\n")
		#  Display result
		print(" Sorted Word : ", output, "\n")
	end

end

def main() 
	task = Sorting.new()
	text = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries"
	task.sortWord(text)
end

main()

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
import scala.collection.mutable._;
/*
    Scala Program for
    Sort words of string in ascending order
*/
class Sorting()
{
	def sortWord(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		var words: Array[String] = text.split(" ");
		// Sort word value
		words = words.sorted;
		var output: String = "";
		var i: Int = 0;
		// Collect sort word
		while (i < words.length)
		{
			if (i != 0)
			{
				output += " ";
			}
			output += words(i);
			i += 1;
		}
		// Display given text
		println(" Given Text  : " + text);
		// Display result
		println(" Sorted Word : " + output);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Sorting = new Sorting();
		var text: String = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
		task.sortWord(text);
	}
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
import Foundation;
/*
    Swift 4 Program for
    Sort words of string in ascending order
*/
class Sorting
{
	func sortWord(_ text: String)
	{
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		var words: [String] = text.split
		{
			$0 == " "
		}.map(String.init);
		// Sort word value
		words = words.sorted();
		var output: String = "";
		var i: Int = 0;
		// Collect sort word
		while (i < words.count)
		{
			if (i  != 0)
			{
				output += " ";
			}
			output += words[i];
			i += 1;
		}
		// Display given text
		print(" Given Text  : ", text);
		// Display result
		print(" Sorted Word : ", output);
	}
}
func main()
{
	let task: Sorting = Sorting();
	let text: String = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
	task.sortWord(text);
}
main();

Output

 Given Text  :  Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word :  Apple Banna Blueberries Cherries Durian Grapes Manguage Orange
/*
    Kotlin Program for
    Sort words of string in ascending order
*/
class Sorting
{
	fun sortWord(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		// Spit text by space
		var words: Array < String > = text.split(" ").toTypedArray();
		// Sort word value
		words.sort();
		var output: String = "";
		var i: Int = 0;
		// Collect sort word
		while (i < words.count())
		{
			if (i != 0)
			{
				output += " ";
			}
			output += words[i];
			i += 1;
		}
		// Display given text
		println(" Given Text  : " + text);
		// Display result
		println(" Sorted Word : " + output);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Sorting = Sorting();
	val text: String = "Manguage Cherries Durian Apple Banna Grapes Orange Blueberries";
	task.sortWord(text);
}

Output

 Given Text  : Manguage Cherries Durian Apple Banna Grapes Orange Blueberries
 Sorted Word : Apple Banna Blueberries Cherries Durian Grapes Manguage Orange


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved