Print last character of each word in a string
Given a 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 ?> |
s r s
Recommended Posts:
- Print the first and last character of each word in a String
- Capitalize the first and last character of each word in a string
- Remove the first and last character of each word in a string
- Reverse every word of the string except the first and the last character
- Print the string after the specified character has occurred given no. of times
- Print Kth character in sorted concatenated substrings of a string
- Print the string by ignoring alternate occurrences of any character
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Most frequent word in first String which is not present in second String
- String containing first letter of every word in a given string with spaces
- Replace every character of a string by a different character
- Print list items containing all characters of a given word
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.



