Print last character of every word in a string
Here given code implementation process.
/*
Java program for
Print last character of every word in a string
*/
public class Find
{
public void printLastCharacter(String text)
{
// Get the length
int n = text.length();
if (n == 0)
{
return;
}
System.out.println("Given Text : " + text);
// Collecting words
String[] word = text.split(" ");
for (int i = 0; i < word.length; ++i)
{
if (word[i].length() > 0)
{
if (i != 0)
{
System.out.print(" ");
}
System.out.print(word[i].charAt(word[i].length() - 1));
}
}
}
public static void main(String[] args)
{
Find task = new Find();
String text = "I lost my control in this game";
task.printLastCharacter(text);
}
}
Output
Given Text : I lost my control in this game
I t y l n s e
package main
import "fmt"
import "strings"
/*
Go program for
Print last character of every word in a string
*/
type Find struct {}
func getFind() * Find {
var me *Find = &Find {}
return me
}
func(this Find) printLastCharacter(text string) {
// Get the length
var n int = len(text)
if n == 0 {
return
}
fmt.Println("Given Text : ", text)
// Collecting words
var word = strings.Split(text, " ")
for i := 0 ; i < len(word) ; i++ {
if len(word[i]) > 0 {
if i != 0 {
fmt.Print(" ")
}
fmt.Printf("%c",word[i][len(word[i]) - 1])
}
}
}
func main() {
var task * Find = getFind()
var text string = "I lost my control in this game"
task.printLastCharacter(text)
}
Output
Given Text : I lost my control in this game
I t y l n s e
// Include namespace system
using System;
/*
Csharp program for
Print last character of every word in a string
*/
public class Find
{
public void printLastCharacter(String text)
{
// Get the length
int n = text.Length;
if (n == 0)
{
return;
}
Console.WriteLine("Given Text : " + text);
// Collecting words
String[] word = text.Split(" ");
for (int i = 0; i < word.Length; ++i)
{
if (word[i].Length > 0)
{
if (i != 0)
{
Console.Write(" ");
}
Console.Write(word[i][word[i].Length - 1]);
}
}
}
public static void Main(String[] args)
{
Find task = new Find();
String text = "I lost my control in this game";
task.printLastCharacter(text);
}
}
Output
Given Text : I lost my control in this game
I t y l n s e
<?php
/*
Php program for
Print last character of every word in a string
*/
class Find
{
public function printLastCharacter($text)
{
// Get the length
$n = strlen($text);
if ($n == 0)
{
return;
}
echo("Given Text : ".$text.
"\n");
// Collecting words
$word = explode(" ", $text);
for ($i = 0; $i < count($word); ++$i)
{
if (strlen($word[$i]) > 0)
{
if ($i != 0)
{
echo(" ");
}
echo($word[$i][strlen($word[$i]) - 1]);
}
}
}
}
function main()
{
$task = new Find();
$text = "I lost my control in this game";
$task->printLastCharacter($text);
}
main();
Output
Given Text : I lost my control in this game
I t y l n s e
/*
Node JS program for
Print last character of every word in a string
*/
class Find
{
printLastCharacter(text)
{
// Get the length
var n = text.length;
if (n == 0)
{
return;
}
console.log("Given Text : " + text);
// Collecting words
var word = text.split(" ");
for (var i = 0; i < word.length; ++i)
{
if (word[i].length > 0)
{
if (i != 0)
{
process.stdout.write(" ");
}
process.stdout.write(word[i].charAt(word[i].length - 1));
}
}
}
}
function main()
{
var task = new Find();
var text = "I lost my control in this game";
task.printLastCharacter(text);
}
main();
Output
Given Text : I lost my control in this game
I t y l n s e
# Python 3 program for
# Print last character of every word in a string
class Find :
def printLastCharacter(self, text) :
# Get the length
n = len(text)
if (n == 0) :
return
print("Given Text : ", text)
# Collecting words
word = text.split(" ")
i = 0
while (i < len(word)) :
if (len(word[i]) > 0) :
if (i != 0) :
print(" ", end = "")
print(word[i][len(word[i]) - 1], end = "")
i += 1
def main() :
task = Find()
text = "I lost my control in this game"
task.printLastCharacter(text)
if __name__ == "__main__": main()
Output
Given Text : I lost my control in this game
I t y l n s e
# Ruby program for
# Print last character of every word in a string
class Find
def printLastCharacter(text)
# Get the length
n = text.length
if (n == 0)
return
end
print("Given Text : ", text, "\n")
# Collecting words
word = text.split(" ")
i = 0
while (i < word.length)
if (word[i].length > 0)
if (i != 0)
print(" ")
end
print(word[i][word[i].length - 1])
end
i += 1
end
end
end
def main()
task = Find.new()
text = "I lost my control in this game"
task.printLastCharacter(text)
end
main()
Output
Given Text : I lost my control in this game
I t y l n s e
import scala.collection.mutable._;
/*
Scala program for
Print last character of every word in a string
*/
class Find()
{
def printLastCharacter(text: String): Unit = {
// Get the length
var n: Int = text.length();
if (n == 0)
{
return;
}
println("Given Text : " + text);
// Collecting words
var word: Array[String] = text.split(" ");
var i: Int = 0;
while (i < word.length)
{
if (word(i).length() > 0)
{
if (i != 0)
{
print(" ");
}
print(word(i).charAt(word(i).length() - 1));
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Find = new Find();
var text: String = "I lost my control in this game";
task.printLastCharacter(text);
}
}
Output
Given Text : I lost my control in this game
I t y l n s e
import Foundation;
/*
Swift 4 program for
Print last character of every word in a string
*/
class Find
{
func printLastCharacter(_ text: String)
{
// Get the length
let n: Int = text.count;
if (n == 0)
{
return;
}
print("Given Text : ", text);
// Collecting words
let word: [String] = text.split
{
$0 == " "
}.map(String.init);
var i: Int = 0;
while (i < word.count)
{
if (word[i].count > 0)
{
if (i != 0)
{
print(" ", terminator: "");
}
print(Array(word[i])[word[i].count - 1], terminator: "");
}
i += 1;
}
}
}
func main()
{
let task: Find = Find();
let text: String = "I lost my control in this game";
task.printLastCharacter(text);
}
main();
Output
Given Text : I lost my control in this game
I t y l n s e
/*
Kotlin program for
Print last character of every word in a string
*/
class Find
{
fun printLastCharacter(text: String): Unit
{
// Get the length
val n: Int = text.length;
if (n == 0)
{
return;
}
println("Given Text : " + text);
// Collecting words
val word = text.split(" ");
var i: Int = 0;
while (i < word.count())
{
if (word[i].length > 0)
{
if (i != 0)
{
print(" ");
}
print(word[i].get(word[i].length - 1));
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Find = Find();
val text: String = "I lost my control in this game";
task.printLastCharacter(text);
}
Output
Given Text : I lost my control in this game
I t y l n s e
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