Sort characters in a string

Here given code implementation process.

import java.util.Arrays;
/*
    Java program for
    Sort characters in a string
*/
public class Sorting
{
	public String sortText(String data)
	{
		char[] s = data.toCharArray();
		// Sort element
		Arrays.sort(s);
		return new String(s);
	}
	public static void main(String[] args)
	{
		Sorting task = new Sorting();
		String d1 = "manager";
		String d2 = "oksdsflkfdfrxsdfs sdfs";
		String d3 = "AfdvfGdsffdsfrweasc";
		String d4 = "coding";
		// Test
		d1 = task.sortText(d1);
		d2 = task.sortText(d2);
		d3 = task.sortText(d3);
		d4 = task.sortText(d4);
      	// Display values
		System.out.println(d1);
		System.out.println(d2);
		System.out.println(d3);
		System.out.println(d4);
	}
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
// Include header file
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
/*
    C++ program for
    Sort characters in a string
*/
class Sorting
{
	public: string sortText(string s)
	{
		// Sort element
		sort(s.begin(), s.end());
		return s;
	}
};
int main()
{
	Sorting *task = new Sorting();
	string d1 = "manager";
	string d2 = "oksdsflkfdfrxsdfs sdfs";
	string d3 = "AfdvfGdsffdsfrweasc";
	string d4 = "coding";
	// Test
	d1 = task->sortText(d1);
	d2 = task->sortText(d2);
	d3 = task->sortText(d3);
	d4 = task->sortText(d4);
	// Display values
	cout << d1 << endl;
	cout << d2 << endl;
	cout << d3 << endl;
	cout << d4 << endl;
	return 0;
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
// Include namespace system
using System;
/*
    Csharp program for
    Sort characters in a string
*/
public class Sorting
{
	public String sortText(String data)
	{
		char[] s = data.ToCharArray();
		// Sort element
		Array.Sort(s);
		return new String(s);
	}
	public static void Main(String[] args)
	{
		Sorting task = new Sorting();
		String d1 = "manager";
		String d2 = "oksdsflkfdfrxsdfs sdfs";
		String d3 = "AfdvfGdsffdsfrweasc";
		String d4 = "coding";
		// Test
		d1 = task.sortText(d1);
		d2 = task.sortText(d2);
		d3 = task.sortText(d3);
		d4 = task.sortText(d4);
		// Display values
		Console.WriteLine(d1);
		Console.WriteLine(d2);
		Console.WriteLine(d3);
		Console.WriteLine(d4);
	}
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
package main
import "sort"
import "strings"
import "fmt"
/*
    Go program for
    Sort characters in a string
*/

func sortText(str string) string {
    s := strings.Split(str, "")
    sort.Strings(s)
    return strings.Join(s, "")
}
func main() {
	var d1 string = "manager"
	var d2 string = "oksdsflkfdfrxsdfs sdfs"
	var d3 string = "AfdvfGdsffdsfrweasc"
	var d4 string = "coding"
	// Test
	d1 = sortText(d1)
	d2 = sortText(d2)
	d3 = sortText(d3)
	d4 = sortText(d4)
	// Display values
	fmt.Println(d1)
	fmt.Println(d2)
	fmt.Println(d3)
	fmt.Println(d4)
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
<?php
/*
    Php program for
    Sort characters in a string
*/
class Sorting
{
	public	function sortText($data)
	{
		$s = str_split($data);
		// Sort element
		sort($s);
		return implode($s);
	}
}

function main()
{
	$task = new Sorting();
	$d1 = "manager";
	$d2 = "oksdsflkfdfrxsdfs sdfs";
	$d3 = "AfdvfGdsffdsfrweasc";
	$d4 = "coding";
	// Test
	$d1 = $task->sortText($d1);
	$d2 = $task->sortText($d2);
	$d3 = $task->sortText($d3);
	$d4 = $task->sortText($d4);
	// Display values
	echo($d1.
		"\n");
	echo($d2.
		"\n");
	echo($d3.
		"\n");
	echo($d4.
		"\n");
}
main();

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
/*
    Node JS program for
    Sort characters in a string
*/
class Sorting
{
	sortText(s)
    {
		return s.split('').sort().join('');;
	}
}

function main()
{
	var task = new Sorting();
	var d1 = "manager";
	var d2 = "oksdsflkfdfrxsdfs sdfs";
	var d3 = "AfdvfGdsffdsfrweasc";
	var d4 = "coding";
	// Test
	d1 = task.sortText(d1);
	d2 = task.sortText(d2);
	d3 = task.sortText(d3);
	d4 = task.sortText(d4);
	// Display values
	console.log(d1);
	console.log(d2);
	console.log(d3);
	console.log(d4);
}
main();

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
#    Python 3 program for
#    Sort characters in a string
class Sorting :
	def sortText(self, data) :

		#  Sort element
		return ''.join(sorted(data)) 
	

def main() :
	task = Sorting()
	d1 = "manager"
	d2 = "oksdsflkfdfrxsdfs sdfs"
	d3 = "AfdvfGdsffdsfrweasc"
	d4 = "coding"
	#  Test
	d1 = task.sortText(d1)
	d2 = task.sortText(d2)
	d3 = task.sortText(d3)
	d4 = task.sortText(d4)
	#  Display values
	print(d1)
	print(d2)
	print(d3)
	print(d4)

if __name__ == "__main__": main()

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
#    Ruby program for
#    Sort characters in a string
class Sorting 
	def sortText(data) 
		#  Sort element
		return (data.chars.sort.join)
	end

end

def main() 
	task = Sorting.new()
	d1 = "manager"
	d2 = "oksdsflkfdfrxsdfs sdfs"
	d3 = "AfdvfGdsffdsfrweasc"
	d4 = "coding"
	#  Test
	d1 = task.sortText(d1)
	d2 = task.sortText(d2)
	d3 = task.sortText(d3)
	d4 = task.sortText(d4)
	#  Display values
	print(d1, "\n")
	print(d2, "\n")
	print(d3, "\n")
	print(d4, "\n")
end

main()

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
import scala.collection.mutable._;
/*
    Scala program for
    Sort characters in a string
*/
class Sorting()
{
	def sortText(data: String): String = {
		var s: Array[Char] = data.toCharArray();
		// Sort element
		s = s.sorted;
		return new String(s);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Sorting = new Sorting();
		var d1: String = "manager";
		var d2: String = "oksdsflkfdfrxsdfs sdfs";
		var d3: String = "AfdvfGdsffdsfrweasc";
		var d4: String = "coding";
		// Test
		d1 = task.sortText(d1);
		d2 = task.sortText(d2);
		d3 = task.sortText(d3);
		d4 = task.sortText(d4);
		// Display values
		println(d1);
		println(d2);
		println(d3);
		println(d4);
	}
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
import Foundation;
/*
    Swift 4 program for
    Sort characters in a string
*/
class Sorting
{
	func sortText(_ data: String) -> String
	{
		var s: [Character] = Array(data);
		// Sort element
		s = s.sorted();
		var i = 0;
		var v = "";
		while (i < s.count)
		{
			v += String(s[i]);
			i += 1;
		}
		return String(s);
	}
}
func main()
{
	let task: Sorting = Sorting();
	var d1: String = "manager";
	var d2: String = "oksdsflkfdfrxsdfs sdfs";
	var d3: String = "AfdvfGdsffdsfrweasc";
	var d4: String = "coding";
	// Test
	d1 = task.sortText(d1);
	d2 = task.sortText(d2);
	d3 = task.sortText(d3);
	d4 = task.sortText(d4);
	// Display values
	print(d1);
	print(d2);
	print(d3);
	print(d4);
}
main();

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino
/*
    Kotlin program for
    Sort characters in a string
*/
class Sorting
{
	fun sortText(data: String): String 
	{
		val s: Array < Char > = data.toCharArray().toTypedArray();
		// Sort element
		s.sort();
		var v = "";
		var i: Int = 0;
		// Append text characters into ans    
		while (i < s.count())
		{
			v += s[i];
			i += 1;
		}
		return v;
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Sorting = Sorting();
	var d1: String = "manager";
	var d2: String = "oksdsflkfdfrxsdfs sdfs";
	var d3: String = "AfdvfGdsffdsfrweasc";
	var d4: String = "coding";
	// Test
	d1 = task.sortText(d1);
	d2 = task.sortText(d2);
	d3 = task.sortText(d3);
	d4 = task.sortText(d4);
	// Display values
	println(d1);
	println(d2);
	println(d3);
	println(d4);
}

Output

aaegmnr
 ddddfffffkklorssssssx
AGacdddefffffrsssvw
cdgino

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