Capitalize the first and last letter of each word of a String

Given a string which contains N words, This word can be from of single and multiple characters. Our goal is to capitalize the first and last letter of each word. for example.

Input  : You are better than me
Output : YoU ArE BetteR ThaN ME

Input  : i have an doubt
Output : I HavE AN DoubT

Here given code implementation process.

/*
    Java program for
    Capitalize the first and last letter of each word of a String
*/
public class Replacement
{
    public String capitalizeFirstAndLast(String text)
    {
        int n = text.length();
        if (n == 1)
        {
            // When have less than one character
            return text.toUpperCase();
        }
        if (n == 2)
        {
            // When only two characters
            return text.substring(0, 1).toUpperCase() + 
              text.substring(1, 2).toUpperCase();
        }
        return text.substring(0, 1).toUpperCase() + 
          text.substring(1, n - 1) + 
          text.substring(n - 1, n).toUpperCase();
    }
    public void changeWord(String text)
    {
        // Get the length
        int n = text.length();
        if (n == 0)
        {
            return;
        }
        // Collecting words
        String[] word = text.split(" ");
        String result = "";
        for (int i = 0; i < word.length; ++i)
        {
            if (i != 0)
            {
                result += " " + capitalizeFirstAndLast(word[i]);
            }
            else
            {
                result = capitalizeFirstAndLast(word[i]);
            }
        }
        // Display given text
        System.out.println("Given Text : " + text);
        // Display calculated result
        System.out.println(result);
    }
    public static void main(String[] args)
    {
        Replacement task = new Replacement();
        String text = "this is real";
        task.changeWord(text);
        text = "make a good code";
        task.changeWord(text);
    }
}

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
package main
import "fmt"
import "strings"
/*
    Go program for
    Capitalize the first and last letter of each word of a String
*/
type Replacement struct {}
func getReplacement() * Replacement {
    var me *Replacement = &Replacement {}
    return me
}
func(this Replacement) capitalizeFirstAndLast(text string) string {
    var n int = len(text)
    if n == 1 {
        // When have less than one character
        return strings.ToUpper(text)
    }
    if n == 2 {
        // When only two characters
        return strings.ToUpper(string(text[0])) + 
                strings.ToUpper(string(text[1]))
    }
    return strings.ToUpper(string(text[0])) + 
        text[1 : n - 1] + 
        strings.ToUpper(string(text[n-1]))
}
func(this Replacement) changeWord(text string) {
    // Get the length
    var n int = len(text)
    if n == 0 {
        return
    }
    // Collecting words
    var word = strings.Split(text, " ")
    var result string = ""
    for i := 0 ; i < len(word) ; i++ {
        if i != 0 {
            result += " "+ this.capitalizeFirstAndLast(word[i])
        } else {
            result = this.capitalizeFirstAndLast(word[i])
        }
    }
    // Display given text
    fmt.Println("Given Text : ", text)
    // Display calculated result
    fmt.Println(result)
}
func main() {
    var task * Replacement = getReplacement()
    var text string = "this is real"
    task.changeWord(text)
    text = "make a good code"
    task.changeWord(text)
}

Output

Given Text :  this is real
ThiS IS ReaL
Given Text :  make a good code
MakE A GooD CodE
// Include namespace system
using System;
/*
    Csharp program for
    Capitalize the first and last letter of each word of a String
*/
public class Replacement
{
    public String capitalizeFirstAndLast(String text)
    {
        int n = text.Length;
        if (n == 1)
        {
            // When have less than one character
            return text.ToUpper();
        }
        if (n == 2)
        {
            // When only two characters
            return text.ToUpper();
        }
        return text.Substring(0, 1 ).ToUpper() + 
          text.Substring(1, n - 2 ) + text.Substring(n - 1, 1 ).ToUpper();
    }
    public void changeWord(String text)
    {
        // Get the length
        int n = text.Length;
        if (n == 0)
        {
            return;
        }
        // Collecting words
        String[] word = text.Split(" ");
        String result = "";
        for (int i = 0; i < word.Length; ++i)
        {
            if (i != 0)
            {
                result += " " + this.capitalizeFirstAndLast(word[i]);
            }
            else
            {
                result = this.capitalizeFirstAndLast(word[i]);
            }
        }
        // Display given text
        Console.WriteLine("Given Text : " + text);
        // Display calculated result
        Console.WriteLine(result);
    }
    public static void Main(String[] args)
    {
        Replacement task = new Replacement();
        String text = "this is real";
        task.changeWord(text);
        text = "make a good code";
        task.changeWord(text);
    }
}

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
<?php
/*
    Php program for
    Capitalize the first and last letter of each word of a String
*/
class Replacement
{
    public  function capitalizeFirstAndLast($text)
    {
        $n = strlen($text);
        if ($n == 1)
        {
            // When have less than one character
            return strtoupper($text);
        }
        if ($n == 2)
        {
            // When only two characters
            return strtoupper(substr($text, 0, 1)).strtoupper(
              substr($text, 1, 2 - 1));
        }
        return strtoupper(substr($text, 0, 1 )).substr(
          $text, 1, $n - 2).strtoupper(substr($text, $n - 1, 1));
    }
    public  function changeWord($text)
    {
        // Get the length
        $n = strlen($text);
        if ($n == 0)
        {
            return;
        }
        // Collecting words
        $word = explode(" ", $text);
        $result = "";
        for ($i = 0; $i < count($word); ++$i)
        {
            if ($i != 0)
            {
                $result .= " ".$this->capitalizeFirstAndLast($word[$i]);
            }
            else
            {
                $result = $this->capitalizeFirstAndLast($word[$i]);
            }
        }
        // Display given text
        echo("Given Text : ".$text."\n");
        // Display calculated result
        echo($result."\n");
    }
}

function main()
{
    $task = new Replacement();
    $text = "this is real";
    $task->changeWord($text);
    $text = "make a good code";
    $task->changeWord($text);
}
main();

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
/*
    Node JS program for
    Capitalize the first and last letter of each word of a String
*/
class Replacement
{
    capitalizeFirstAndLast(text)
    {
        var n = text.length;
        if (n == 1)
        {
            // When have less than one character
            return text.toUpperCase();
        }
        if (n == 2)
        {
            // When only two characters
            return text.substring(0, 1).toUpperCase() + 
              text.substring(1, 2).toUpperCase();
        }
        return text.substring(0, 1).toUpperCase() + 
          text.substring(1, n - 1) + text.substring(n - 1, n).toUpperCase();
    }
    changeWord(text)
    {
        // Get the length
        var n = text.length;
        if (n == 0)
        {
            return;
        }
        // Collecting words
        var word = text.split(" ");
        var result = "";
        for (var i = 0; i < word.length; ++i)
        {
            if (i != 0)
            {
                result += " " + this.capitalizeFirstAndLast(word[i]);
            }
            else
            {
                result = this.capitalizeFirstAndLast(word[i]);
            }
        }
        // Display given text
        console.log("Given Text : " + text);
        // Display calculated result
        console.log(result);
    }
}

function main()
{
    var task = new Replacement();
    var text = "this is real";
    task.changeWord(text);
    text = "make a good code";
    task.changeWord(text);
}
main();

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
#    Python 3 program for
#    Capitalize the first and last letter of each word of a String
class Replacement :
    def capitalizeFirstAndLast(self, text) :
        n = len(text)
        if (n == 1) :
            #  When have less than one character
            return text.upper()
        
        if (n == 2) :
            #  When only two characters
            return text[0: 1].upper() + text[1: 2].upper()
        
        return text[0: 1].upper() + text[1: n-1] + text[n - 1: n].upper()
    
    def changeWord(self, text) :
        #  Get the length
        n = len(text)
        if (n == 0) :
            return
        
        #  Collecting words
        word = text.split(" ")
        result = ""
        i = 0
        while (i < len(word)) :
            if (i != 0) :
                result += " " + self.capitalizeFirstAndLast(word[i])
            else :
                result = self.capitalizeFirstAndLast(word[i])
            
            i += 1
        
        #  Display given text
        print("Given Text : ", text)
        #  Display calculated result
        print(result)
    

def main() :
    task = Replacement()
    text = "this is real"
    task.changeWord(text)
    text = "make a good code"
    task.changeWord(text)

if __name__ == "__main__": main()

Output

Given Text :  this is real
ThiS IS ReaL
Given Text :  make a good code
MakE A GooD CodE
#    Ruby program for
#    Capitalize the first and last letter of each word of a String
class Replacement 
    def capitalizeFirstAndLast(text) 
        n = text.length
        if (n == 1) 
            #  When have less than one character
            return text.upcase
        end

        if (n == 2) 
            #  When only two characters
            return text[0].upcase + text[1].upcase
        end

        return text[0].upcase + text[1...(n - 1)] + text[n - 1].upcase
    end

    def changeWord(text) 
        #  Get the length
        n = text.length
        if (n == 0) 
            return
        end

        #  Collecting words
        word = text.split(" ")
        result = ""
        i = 0
        while (i < word.length) 
            if (i != 0) 
                result += " "+ self.capitalizeFirstAndLast(word[i])
            else
 
                result = self.capitalizeFirstAndLast(word[i])
            end

            i += 1
        end

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

end

def main() 
    task = Replacement.new()
    text = "this is real"
    task.changeWord(text)
    text = "make a good code"
    task.changeWord(text)
end

main()

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
import scala.collection.mutable._;
/*
    Scala program for
    Capitalize the first and last letter of each word of a String
*/
class Replacement()
{
    def capitalizeFirstAndLast(text: String): String = {
        var n: Int = text.length();
        if (n == 1)
        {
            // When have less than one character
            return text.toUpperCase();
        }
        if (n == 2)
        {
            // When only two characters
            return text.substring(0, 1).toUpperCase() + 
              text.substring(1, 2).toUpperCase();
        }
        return text.substring(0, 1).toUpperCase() + 
          text.substring(1, n - 1) + text.substring(n - 1, n).toUpperCase();
    }
    def changeWord(text: String): Unit = {
        // Get the length
        var n: Int = text.length();
        if (n == 0)
        {
            return;
        }
        // Collecting words
        var word: Array[String] = text.split(" ");
        var result: String = "";
        var i: Int = 0;
        while (i < word.length)
        {
            if (i != 0)
            {
                result += " " + capitalizeFirstAndLast(word(i));
            }
            else
            {
                result = capitalizeFirstAndLast(word(i));
            }
            i += 1;
        }
        // Display given text
        println("Given Text : " + text);
        // Display calculated result
        println(result);
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Replacement = new Replacement();
        var text: String = "this is real";
        task.changeWord(text);
        text = "make a good code";
        task.changeWord(text);
    }
}

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE
import Foundation;
/*
    Swift 4 program for
    Capitalize the first and last letter of each word of a String
*/
class Replacement
{
    func getMiddle(_ text: [Character]) -> String
    {
        var i = 1;
        var value = "";
        while (i < text.count - 1)
        {
            value += String(text[i]);
            i+=1;
        }
      return value;
    }
    func capitalizeFirstAndLast(_ text: [Character]) -> String
    {
        let n: Int = text.count;
        if (n == 1)
        {
            // When have less than one character
            return String(text[0]).uppercased();
        }
        if (n == 2)
        {
            // When only two characters
            return String(text[0]).uppercased() + String(text[1]).uppercased();
        }
        return String(text[0]).uppercased() + 
          getMiddle(text) + String(text[n - 1]).uppercased();
}
func changeWord(_ text: String)
{
    // Get the length
    let n: Int = text.count;
    if (n == 0)
    {
        return;
    }
    // Collecting words
    let word: [String] = text.split
    {
        $0 == " "
    }.map(String.init);
    var result: String = "";
    var i: Int = 0;
    while (i < word.count)
    {
        if (i  != 0)
        {
            result += " " + self.capitalizeFirstAndLast(Array(word[i]));
        }
        else
        {
            result = self.capitalizeFirstAndLast(Array(word[i]));
        }
        i += 1;
    }
    // Display given text
    print("Given Text : ", text);
    // Display calculated result
    print(result);
}
}
func main()
{
    let task: Replacement = Replacement();
    var text: String = "this is real";
    task.changeWord(text);
    text = "make a good code";
    task.changeWord(text);
}
main();

Output

Given Text :  this is real
ThiS IS ReaL
Given Text :  make a good code
MakE A GooD CodE
/*
    Kotlin program for
    Capitalize the first and last letter of each word of a String
*/
class Replacement
{
    fun capitalizeFirstAndLast(text: String): String 
    {
        val n: Int = text.length;
        if (n == 1)
        {
            // When have less than one character
            return text.toUpperCase();
        }
        if (n == 2)
        {
            // When only two characters
            return text.toUpperCase();
        }
        return text.substring(0, 1).toUpperCase() + 
          text.substring(1, n - 1) + text.substring(n - 1, n).toUpperCase();
    }
    fun changeWord(text: String): Unit
    {
        // Get the length
        val n: Int = text.length;
        if (n == 0)
        {
            return;
        }
        // Collecting words
        val word = text.split(" ");
        var result: String = "";
        var i: Int = 0;
        while (i < word.count())
        {
            if (i != 0)
            {
                result += " " + this.capitalizeFirstAndLast(word[i]);
            }
            else
            {
                result = this.capitalizeFirstAndLast(word[i]);
            }
            i += 1;
        }
        // Display given text
        println("Given Text : " + text);
        // Display calculated result
        println(result);
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Replacement = Replacement();
    var text: String = "this is real";
    task.changeWord(text);
    text = "make a good code";
    task.changeWord(text);
}

Output

Given Text : this is real
ThiS IS ReaL
Given Text : make a good code
MakE A GooD CodE


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