Capitalize the first and last character of each word in a string

Given the string, the task is to capitalise the first and last character of each word in a string.

Examples:

Input: Geeks for geeks
Output: GeekS FoR GeekS

Input: Geeksforgeeks is best
Output: GeeksforgeekS IS BesT

Approach



  • Create a character array of the String
  • Run a loop from the first letter to the last letter.
  • Check if the character is the starting or end of the word
  • Check if the character is a small letter.
  • If yes, then Capitalise the character of the string.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to capitalise the first
// and last character of each word in a string.
#include<bits/stdc++.h>
using namespace std;
  
string FirstAndLast(string str)
{
  
    // Create an equivalent string
    // of the given string
    string ch = str;
    for (int i = 0; i < ch.length(); i++)
    {
  
        // k stores index of first character
        // and i is going to store index of last
        // character.
        int k = i;
        while (i < ch.length() && ch[i] != ' ')
            i++;
  
        // Check if the character is a small letter
        // If yes, then Capitalise
        ch[k] = (char)(ch[k] >= 'a' && ch[k] <= 'z'
                        ? ((int)ch[k] - 32)
                        : (int)ch[k]);
        ch[i - 1] = (char)(ch[i - 1] >= 'a' && ch[i - 1] <= 'z'
                            ? ((int)ch[i - 1] - 32)
                            : (int)ch[i - 1]);
    }
  
    return ch;
}
  
// Driver code
int main()
{
    string str = "Geeks for Geeks";
    cout << str << "\n";
    cout << FirstAndLast(str);
}
  
// This code is contributed by ihritik

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to capitalise the first
// and last character of each word in a string.
  
class GFG {
    static String FirstAndLast(String str)
    {
  
        // Create an equivalent char array
        // of given string
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
  
            // k stores index of first character
            // and i is going to store index of last
            // character.
            int k = i;
            while (i < ch.length && ch[i] != ' ')
                i++;
  
            // Check if the character is a small letter
            // If yes, then Capitalise
            ch[k] = (char)(ch[k] >= 'a' && ch[k] <= 'z'
                               ? ((int)ch[k] - 32)
                               : (int)ch[k]);
            ch[i - 1] = (char)(ch[i - 1] >= 'a' && ch[i - 1] <= 'z'
                                   ? ((int)ch[i - 1] - 32)
                                   : (int)ch[i - 1]);
        }
  
        return new String(ch);
    }
  
    // Driver code
    public static void main(String args[])
    {
        String str = "Geeks for Geeks";
        System.out.println(str);
        System.out.println(FirstAndLast(str));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to capitalise the first 
# and last character of each word in a string. 
def FirstAndLast(string) : 
      
    # Create an equivalent char array 
    # of given string 
    ch = list(string); 
      
    i = 0 ;
    while i < len(ch):
  
        # k stores index of first character 
        # and i is going to store index of last 
        # character. 
        k = i; 
          
        while (i < len(ch) and ch[i] != ' ') :
            i += 1
  
        # Check if the character is a small letter 
        # If yes, then Capitalise 
        if (ord(ch[k]) >= 97 and 
            ord(ch[k]) <= 122 ):
            ch[k] = chr(ord(ch[k]) - 32);
        else :
            ch[k] = ch[k]
              
        if (ord(ch[i - 1]) >= 90 and 
            ord(ch[i - 1]) <= 122 ):
            ch[i - 1] = chr(ord(ch[i - 1]) - 32);
        else :
            ch[i - 1] = ch[i - 1]
              
        i += 1
          
    return "" . join(ch); 
      
# Driver code 
if __name__ == "__main__" :
      
    string = "Geeks for Geeks"
      
    print(string); 
    print(FirstAndLast(string)); 
      
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to remove the first
// and last character of each word in a string.
using System;
  
class GFG 
{
    static String FirstAndLast(String str)
    {
  
        // Create an equivalent char array
        // of given string
        char[] ch = str.ToCharArray();
        for (int i = 0; i < ch.Length; i++)
        {
  
            // k stores index of first character
            // and i is going to store index of last
            // character.
            int k = i;
            while (i < ch.Length && ch[i] != ' ')
                i++;
  
            // Check if the character is a small letter
            // If yes, then Capitalise
            ch[k] = (char)(ch[k] >= 'a' && ch[k] <= 'z'
                            ? ((int)ch[k] - 32)
                            : (int)ch[k]);
            ch[i - 1] = (char)(ch[i - 1] >= 'a' && ch[i - 1] <= 'z'
                                ? ((int)ch[i - 1] - 32)
                                : (int)ch[i - 1]);
        }
  
        return new String(ch);
    }
  
    // Driver code
    public static void Main(String []args)
    {
        String str = "Geeks for Geeks";
        Console.WriteLine(str);
        Console.WriteLine(FirstAndLast(str));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to capitalise the first
// and last character of each word in a string.
  
function FirstAndLast($str)
{
  
    // Create an equivalent string
    // of the given string
    $ch = $str;
    for ($i = 0; $i < strlen($ch); $i++)
    {
  
        // $k stores index of first character
        // and $i is going to store index of last
        // character.
        $k = $i;
        while ($i < strlen($ch) && $ch[$i] != ' ')
            $i++;
  
        // Check if the character is a small letter
        // If yes, then Capitalise
        $ch[$k] = chr(($ch[$k] >= 'a' && $ch[$k] <= 'z'
                    ? (ord($ch[$k]) - 32) : (ord($ch[$k])));
        $ch[$i - 1] = chr(($ch[$i - 1] >= 'a' && $ch[$i - 1] <= 'z' )
                    ? (ord($ch[$i - 1]) - 32) : (ord($ch[$i - 1])));
    }
  
    return $ch;
}
  
// Driver code
  
$str = "Geeks for Geeks";
echo $str, "\n";
echo FirstAndLast($str);
  
// This code is contributed by ihritik
  
?>

chevron_right


Output:

Geeks for Geeks
GeekS FoR GeekS


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.