Open In App

Print last character of each word in a string

Last Updated : 25 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
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)



Similar Reads

Print the first and last character of each word in a String
Given a string, the task is to print the first and last character of each word in a string.Examples: Input: Geeks for geeks Output: Gs fr gs Input: Computer applications Output: Cr as Approach Run a loop from the first letter to the last letter.Print the first and last letter of the string.If there is a space in the string then print the character
7 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 the character is the starting or end of the wordR
4 min read
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 StringRun a loop from the first letter to the last letter.Check if the character is the startin
6 min read
Repeat last occurrence of each alphanumeric character to their position in character family times
Given a string str[] of size N, the task is to encode it in such a way that the last occurrence of each character occurs as long as its position in its family. As 'a' is the first character of its family (lower case alphabet), so it will remain 'a', but 'b' becomes 'bb', 'D' becomes 'DDDD' and so on. In case of numeric characters, the character occ
12 min read
Reverse every word of the string except the first and the last character
Given string str consisting of a sentence, the task is to reverse every word of the sentence except the first and last character of the words. Examples: Input: str = "geeks for geeks" Output: gkees for gkees Input: str = "this is a string" Output: this is a snirtg Approach: Break the string into words using strtok(), now for every word take two poi
5 min read
Modify string by increasing each character by its distance from the end of the word
Given a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word) Examples: Input: S = "acm fkz" Output: "cdm hlz" Explanation: There are 2 words in the given string {"acm", "fkz"} For "acm": a becomes 'a' + 2 = 'c' c becomes 'c' + 1 = 'd' m be
7 min read
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String
Given a Binary string str of length N, the task is to find the last character removed from the string by repeatedly removing the first character of the string and flipping all the characters of the string if the removed character is '0'. Examples: Input: str = "1001" Output: '0' Explanation: Removing str[0] from the string modifies str to "001". Re
5 min read
C++ Program to Print the First Letter of Each Word of a String
String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space. Examples: Input: str = "geeks for geeks" Output: gfg Input: str = "happy coding" Output: hc Source: https://www.geeksforgeeks.org/amaz
4 min read
Kth character after replacing each character of String by its frequency exactly X times
Given a string S consisting of N digits from [1, 9] and a positive integer K and X. Every day each character of the string is replaced by its frequency and value. The task is to find the Kth character of the string after X days. Examples: Input: S = "1214", K = 10, X = 3Output: 4Explanation:1st day = "12214444"2nd day = “1222214444444444444444”3rd
8 min read
Print the last character of lexicographically smallest non-palindromic permutation of a string
Given string str, the task is to print the last character of the lexicographically smallest non-palindromic permutation of the given string. If no such permutation exists, print "-1". Examples: Input: str = "deepqvu"Output: vExplanation: The string "deepquv" is the lexicographically smallest permutation which is not a palindrome.Therefore, the last
13 min read
Practice Tags :