Print last character of each word in a string

31 May, 2021
Given string str, the task is to print the last character of each word in the string.
Examples:  
Input: str = “Geeks for Geeks” Output: s r s
Input: str = “computer applications” Output: r s 
Approach: Append a space i.e. ” “ at the end of the given string so that the last word in the string is also followed by a space just like all the other words in the string. Now start traversing the string character by character, and print every character which is followed by a space.Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// CPP implementation of the approach#include<bits/stdc++.h>using namespace std; // Function to print the last character// of each word in the given stringvoid printLastChar(string str){     // Now, last word is also followed by a space    str = str + " ";    for (int i = 1; i < str.length(); i++)    {         // If current character is a space        if (str[i] == ' ')             // Then previous character must be            // the last character of some word            cout << str[i - 1] << " ";    }} // Driver codeint main(){    string str = "Geeks for Geeks";    printLastChar(str);} // This code is contributed by// Surendra_Gangwar
// Java implementation of the approachclass GFG {     // Function to print the last character    // of each word in the given string    static void printLastChar(String str)    {         // Now, last word is also followed by a space        str = str + " ";        for (int i = 1; i < str.length(); i++) {             // If current character is a space            if (str.charAt(i) == ' ')                 // Then previous character must be                // the last character of some word                System.out.print(str.charAt(i - 1) + " ");        }    }     // Driver code    public static void main(String s[])    {        String str = "Geeks for Geeks";        printLastChar(str);    }}
# Function to print the last character# of each word in the given stringdef printLastChar(string):     # Now, last word is also followed by a space    string = string + " "    for i in range(len(string)):         # If current character is a space        if string[i] == ' ':             # Then previous character must be            # the last character of some word            print(string[i - 1], end = " ") # Driver codestring = "Geeks for Geeks"printLastChar(string) # This code is contributed by Shrikant13
// C# implementation of the approachusing System; class GFG{     // Function to print the last character    // of each word in the given string    static void printLastChar(string str)    {         // Now, last word is also followed by a space        str = str + " ";        for (int i = 1; i < str.Length; i++)        {             // If current character is a space            if (str[i] == ' ')                 // Then previous character must be                // the last character of some word                Console.Write(str[i - 1] + " ");        }    }     // Driver code    public static void Main()    {        string str = "Geeks for Geeks";        printLastChar(str);    }} // This code is contributed by Ryuga
<?php// PHP implementation of the approach // Function to print the last character// of each word in the given stringfunction printLastChar($str){    // Now, last word is also followed by a space    $str = $str . " ";    for ($i = 1; $i < strlen($str); $i++)    {        // If current character is a space        if (!strcmp($str[$i], ' '))             // Then previous character must be            // the last character of some word            echo($str[$i - 1] . " ");    }} // Driver code$str = "Geeks for Geeks";printLastChar($str); // This code contributed by PrinciRaj1992?>
<script>      // JavaScript implementation of the approach      // Function to print the last character      // of each word in the given string      function printLastChar(str) {        // Now, last word is also followed by a space        str = str + " ";        for (var i = 1; i < str.length; i++) {          // If current character is a space          if (str[i] === " ")            // Then previous character must be            // the last character of some word            document.write(str[i - 1] + " ");        }      }       // Driver code      var str = "Geeks for Geeks";      printLastChar(str);</script>
s r s
 
ankthon
SURENDRA_GANGWAR
shrikanth13
princiraj1992
rdtank
Java Programs
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array
Iterate through List in Java
How to Iterate HashMap in Java?
Iterate Over the Characters of a String in Java
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java