Print last character of each word in a string Last Updated : 25 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // CPP implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the last character // of each word in the given string 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 cout << str[i - 1] << " "; } } // Driver code int main() { string str = "Geeks for Geeks"; printLastChar(str); } // This code is contributed by // Surendra_Gangwar Java // Java implementation of the approach 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.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); } } Python3 # Function to print the last character # of each word in the given string def 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 code string = "Geeks for Geeks" printLastChar(string) # This code is contributed by Shrikant13 C# // C# implementation of the approach using 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 // PHP 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 ($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 ?> JavaScript <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> Output: s r s Time complexity: O(n) where n is the length of the given string.Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Remove the first and last character of each word in a string V Vaibhav_Arora Follow Improve Article Tags : DSA Similar Reads Print the first and last character of each word in a String Given a string s consisting of multiple words, print the first and last character of each word. If a word contains only one character, print it twice (since the first and last characters are the same).Note: The string will not contain leading or trailing spaces.Examples: Input: s = "Geeks for geeks" 5 min read Remove the first and last character of each word in a string Given the string the task is to remove the first and last character of each word in a string.Examples: Input: Geeks for geeksOutput: eek o eek Input: Geeksforgeeks is bestOutput: eeksforgeek es Approach : Split the String based on the spaceRun a loop from the first letter to the last letter.Check if 4 min read Print number of words, vowels and frequency of each character Given a string str with uppercase, lowercase and special characters. The input string is to end with either a space or a dot. The problem is to calculate the number of words, vowels and frequency of each character of the string in a separate line. Example : Input : How Good GOD Is. Output : Number o 8 min read Print list items containing all characters of a given word There is a list of items. Given a specific word, e.g., "sun", print out all the items in list which contain all the characters of "sun". For example if the given word is "sun" and the items are "sunday", "geeksforgeeks", "utensils", ""just" and "sss", then the program should print "sunday" and "uten 7 min read Print all unique words of a String Write a function that takes a String as an argument and prints all unique words in it. Examples: Input: Java is great. Grails is also greatOutput: Java Grails also Approach: The idea is to use the map to keep track of words already occurred. But first, we have to extract all words from a String, as 7 min read Length Of Last Word in a String Given a string s consisting of upper/lower-case alphabets and empty space characters ' ', return the length of the last word in the string. If the last word does not exist, return 0. Examples: Input : str = "Geeks For Geeks"Output : 5length(Geeks)= 5Input : str = "Start Coding Here"Output : 4length( 13 min read Like