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

  1. Run a loop from the first letter to the last letter.
  2. Print the first and last letter of the string.
  3. If there is a space in the string then print the character that lies just before space and just after space.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print
// the first and last character
// of each word in a String'
#include<bits/stdc++.h>
using namespace std;
  
// Function to print the first
// and last character of each word.
void FirstAndLast(string str)
{
    int i;
  
    for (i = 0; i < str.length(); i++) 
    {
        // If it is the first word
        // of the string then print it.
        if (i == 0)
            cout<<str[i];
  
        // If it is the last word of the string
        // then also print it.
        if (i == str.length() - 1)
            cout<<str[i];
  
        // If there is a space
        // print the successor and predecessor
        // to space.
        if (str[i] == ' '
        {
            cout<<str[i-1]<<" "<<str[i+1];
        }
    }
}
  
// Driver code
int main()
{
    string str = "Geeks for Geeks";
    FirstAndLast(str);
}
      
// This code is contributed by
// Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print
// the first and last character
// of each word in a String
  
class GFG {
  
    // Function to print the first
    // and last character of each word.
    static void FirstAndLast(String str)
    {
        int i;
  
        for (i = 0; i < str.length(); i++) {
  
            // If it is the first word
            // of the string then print it.
            if (i == 0)
                System.out.print(str.charAt(i));
  
            // If it is the last word of the string
            // then also print it.
            if (i == str.length() - 1)
                System.out.print(str.charAt(i));
  
            // If there is a space
            // print the successor and predecessor
            // to space.
            if (str.charAt(i) == ' ') {
                System.out.print(str.charAt(i - 1)
                                 + " "
                                 + str.charAt(i + 1));
            }
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        String str = "Geeks for Geeks";
        FirstAndLast(str);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print 
// the first and last character 
// of each word in a String 
using System;
  
class GFG 
  
    // Function to print the first 
    // and last character of each word. 
    static void FirstAndLast(string str) 
    
        int i; 
  
        for (i = 0; i < str.Length; i++) 
        
  
            // If it is the first word 
            // of the string then print it. 
            if (i == 0) 
                Console.Write(str[i]); 
  
            // If it is the last word of the string 
            // then also print it. 
            if (i == str.Length - 1) 
                Console.Write(str[i]); 
  
            // If there is a space 
            // print the successor and predecessor 
            // to space. 
            if (str[i] == ' '
            
                Console.Write(str[i - 1] 
                                + " "
                                + str[i + 1]); 
            
        
    
  
    // Driver code 
    public static void Main() 
    
        string str = "Geeks for Geeks"
        FirstAndLast(str); 
    
  
// This code is contributed by Ryuga

chevron_right


Output:

Gs fr Gs


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.