Print the first and last character of each word in a String
Last Updated :
04 Apr, 2023
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 that lies just before space and just after space.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
void FirstAndLast(string str)
{
int i;
for (i = 0; i < str.length(); i++)
{
if (i == 0)
cout<<str[i];
if (i == str.length() - 1)
cout<<str[i];
if (str[i] == ' ' )
{
cout<<str[i-1]<< " " <<str[i+1];
}
}
}
int main()
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}
|
Java
class GFG {
static void FirstAndLast(String str)
{
int i;
for (i = 0 ; i < str.length(); i++) {
if (i == 0 )
System.out.print(str.charAt(i));
if (i == str.length() - 1 )
System.out.print(str.charAt(i));
if (str.charAt(i) == ' ' ) {
System.out.print(str.charAt(i - 1 )
+ " "
+ str.charAt(i + 1 ));
}
}
}
public static void main(String args[])
{
String str = "Geeks for Geeks" ;
FirstAndLast(str);
}
}
|
Python3
def FirstAndLast(string):
for i in range ( len (string)):
if i = = 0 :
print (string[i], end = "")
if i = = len (string) - 1 :
print (string[i], end = "")
if string[i] = = " " :
print (string[i - 1 ],
string[i + 1 ], end = "")
if __name__ = = "__main__" :
string = "Geeks for Geeks"
FirstAndLast(string)
|
C#
using System;
class GFG
{
static void FirstAndLast( string str)
{
int i;
for (i = 0; i < str.Length; i++)
{
if (i == 0)
Console.Write(str[i]);
if (i == str.Length - 1)
Console.Write(str[i]);
if (str[i] == ' ' )
{
Console.Write(str[i - 1]
+ " "
+ str[i + 1]);
}
}
}
public static void Main()
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}
}
|
Javascript
<script>
function FirstAndLast(str)
{
for ( var i = 0; i < str.length; i++)
{
if (i == 0)
document.write(str[i]);
if (i == str.length - 1)
document.write(str[i]);
if (str[i] === " " ) {
document.write(str[i - 1] + " " + str[i + 1]);
}
}
}
var str = "Geeks for Geeks" ;
FirstAndLast(str);
</script>
|
Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(1)
Another Approach:
In this approach we can split the string into words and then iterate through the words. For each word, we can print its first and last character.
we use stringstream to split the string into words. We then iterate through the words and print the first and last character of each word.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void FirstAndLast(string str)
{
stringstream ss(str);
string word;
while (ss >> word) {
cout << word[0] << word[word.length() - 1] << " " ;
}
}
int main()
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}
|
Java
import java.util.*;
class Main {
public static void firstAndLast(String str) {
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String word = st.nextToken();
System.out.print(word.charAt( 0 ) + "" + word.charAt(word.length() - 1 ) + " " );
}
}
public static void main(String[] args) {
String str = "Geeks for Geeks" ;
firstAndLast(str);
}
}
|
Python3
def FirstAndLast(string):
words = string.split()
for word in words:
print (word[ 0 ] + word[ - 1 ], end = " " )
if __name__ = = '__main__' :
string = "Geeks for Geeks"
FirstAndLast(string)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main( string [] args)
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}
static void FirstAndLast( string str)
{
List< string > words = str.Split( ' ' ).ToList();
foreach ( string word in words)
{
Console.Write(word[0]);
Console.Write(word[word.Length - 1]);
Console.Write( " " );
}
}
}
|
Javascript
function FirstAndLast(str) {
let words = str.split( " " );
let output = "" ;
for (let i = 0; i < words.length; i++) {
output += words[i][0] + words[i][words[i].length - 1] + " " ;
}
console.log(output);
}
let str = "Geeks for Geeks" ;
FirstAndLast(str);
|
Time Complexity: O(N)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...