Swapping corner words in a sentence

Here given code implementation process.

/*
    Java program for
    Swapping corner words in a sentence
*/
public class Replacement
{
	public void swapCornerWord(String text)
	{
		// Get the length
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Collecting words
		String[] word = text.split(" ");
		n = word.length;
		String result = "";
		for (int i = 1; i < n - 1; ++i)
		{
			result += " " + word[i];
		}
		if (n > 1)
		{
			result = word[n - 1] + result + " " + word[0];
		}
		else
		{
			result = word[0];
		}
		System.out.println("Given Text : " + text);
		System.out.println(result);
	}
	public static void main(String[] args)
	{
		Replacement task = new Replacement();
		String text = "This error are very dangerous to all";
		task.swapCornerWord(text);
	}
}

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
package main
import "strings"
import "fmt"
/*
    Go program for
    Swapping corner words in a sentence
*/
type Replacement struct {}
func getReplacement() * Replacement {
	var me *Replacement = &Replacement {}
	return me
}
func(this Replacement) swapCornerWord(text string) {
	// Get the length
	var n int = len(text)
	if n == 0 {
		return
	}
	// Collecting words
	var word = strings.Split(text, " ")
	n = len(word)
	var result string = ""
	for i := 1 ; i < n - 1 ; i++ {
		result += " " + word[i]
	}
	if n > 1 {
		result = word[n - 1] + result + " " + word[0]
	} else {
		result = word[0]
	}
	fmt.Println("Given Text : ", text)
	fmt.Println(result)
}
func main() {
	var task * Replacement = getReplacement()
	var text string = "This error are very dangerous to all"
	task.swapCornerWord(text)
}

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
// Include namespace system
using System;
/*
    Csharp program for
    Swapping corner words in a sentence
*/
public class Replacement
{
	public void swapCornerWord(String text)
	{
		// Get the length
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		// Collecting words
		String[] word = text.Split(" ");
		n = word.Length;
		String result = "";
		for (int i = 1; i < n - 1; ++i)
		{
			result += " " + word[i];
		}
		if (n > 1)
		{
			result = word[n - 1] + result + " " + word[0];
		}
		else
		{
			result = word[0];
		}
		Console.WriteLine("Given Text : " + text);
		Console.WriteLine(result);
	}
	public static void Main(String[] args)
	{
		Replacement task = new Replacement();
		String text = "This error are very dangerous to all";
		task.swapCornerWord(text);
	}
}

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
<?php
/*
    Php program for
    Swapping corner words in a sentence
*/
class Replacement
{
	public	function swapCornerWord($text)
	{
		// Get the length
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		// Collecting words
		$word = explode(" ", $text);
		$n = count($word);
		$result = "";
		for ($i = 1; $i < $n - 1; ++$i)
		{
			$result .= " ".$word[$i];
		}
		if ($n > 1)
		{
			$result = $word[$n - 1].$result.
			" ".$word[0];
		}
		else
		{
			$result = $word[0];
		}
		echo("Given Text : ".$text.
			"\n");
		echo($result.
			"\n");
	}
}

function main()
{
	$task = new Replacement();
	$text = "This error are very dangerous to all";
	$task->swapCornerWord($text);
}
main();

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
/*
    Node JS program for
    Swapping corner words in a sentence
*/
class Replacement
{
	swapCornerWord(text)
	{
		// Get the length
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		// Collecting words
		var word = text.split(" ");
		n = word.length;
		var result = "";
		for (var i = 1; i < n - 1; ++i)
		{
			result += " " + word[i];
		}
		if (n > 1)
		{
			result = word[n - 1] + result + " " + word[0];
		}
		else
		{
			result = word[0];
		}
		console.log("Given Text : " + text);
		console.log(result);
	}
}

function main()
{
	var task = new Replacement();
	var text = "This error are very dangerous to all";
	task.swapCornerWord(text);
}
main();

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
#    Python 3 program for
#    Swapping corner words in a sentence
class Replacement :
	def swapCornerWord(self, text) :
		#  Get the length
		n = len(text)
		if (n == 0) :
			return
		
		#  Collecting words
		word = text.split(" ")
		n = len(word)
		result = ""
		i = 1
		while (i < n - 1) :
			result += " "+ word[i]
			i += 1
		
		if (n > 1) :
			result = word[n - 1] + result + " " + word[0]
		else :
			result = word[0]
		
		print("Given Text : ", text)
		print(result)
	

def main() :
	task = Replacement()
	text = "This error are very dangerous to all"
	task.swapCornerWord(text)

if __name__ == "__main__": main()

Output

Given Text :  This error are very dangerous to all
all error are very dangerous to This
#    Ruby program for
#    Swapping corner words in a sentence
class Replacement 
	def swapCornerWord(text) 
		#  Get the length
		n = text.length
		if (n == 0) 
			return
		end

		#  Collecting words
		word = text.split(" ")
		n = word.length
		result = ""
		i = 1
		while (i < n - 1) 
			result += " " + word[i]
			i += 1
		end

		if (n > 1) 
			result = word[n - 1] + result + " " + word[0]
		else
 
			result = word[0]
		end

		print("Given Text : ", text, "\n")
		print(result, "\n")
	end

end

def main() 
	task = Replacement.new()
	text = "This error are very dangerous to all"
	task.swapCornerWord(text)
end

main()

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
import scala.collection.mutable._;
/*
    Scala program for
    Swapping corner words in a sentence
*/
class Replacement()
{
	def swapCornerWord(text: String): Unit = {
		// Get the length
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		// Collecting words
		var word: Array[String] = text.split(" ");
		n = word.length;
		var result: String = "";
		var i: Int = 1;
		while (i < n - 1)
		{
			result += " " + word(i);
			i += 1;
		}
		if (n > 1)
		{
			result = word(n - 1) + result + " " + word(0);
		}
		else
		{
			result = word(0);
		}
		println("Given Text : " + text);
		println(result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Replacement = new Replacement();
		var text: String = "This error are very dangerous to all";
		task.swapCornerWord(text);
	}
}

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This
/*
    Kotlin program for
    Swapping corner words in a sentence
*/
class Replacement
{
	fun swapCornerWord(text: String): Unit
	{
		// Get the length
		var n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		// Collecting words
		val word = text.split(" ");
		n = word.count();
		var result: String = "";
		var i: Int = 1;
		while (i < n - 1)
		{
			result += " " + word[i];
			i += 1;
		}
		if (n > 1)
		{
			result = word[n - 1] + result + " " + word[0];
		}
		else
		{
			result = word[0];
		}
		println("Given Text : " + text);
		println(result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Replacement = Replacement();
	val text: String = "This error are very dangerous to all";
	task.swapCornerWord(text);
}

Output

Given Text : This error are very dangerous to all
all error are very dangerous to This


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