Write a Java Program to Swap first and last character of words in a Sentence as mentioned in the example?
Input : geeks for geeks Output :seekg rof seekg
Approach:As mentioned in the example we have to replace first and last character of word and keep rest of the alphabets as it is.
- First we will create an Char array of given String by using toCharArray() method.
- Now we iterate the char array by using for loop.
- In for loop, we declare a variable whose value is dependent on i.
- Whenever we found an alphabet we increase the value of i and whenever we reach at space, we are going to perform swapping between first and last character of the word which is previous of space.
seekg rof seekg
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Frequency of smallest character in first sentence less than that of second sentence
- Swap corner words and reverse middle characters
- Swap the first and last character of a string in Java
- Program to check if first and the last characters of string are equal
- Java program to count the characters in each word in a given sentence
- Split a sentence into words in C++
- Count palindrome words in a sentence
- Remove all the palindromic words from the given sentence
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Count substrings with different first and last characters
- Program to convert first character uppercase in a sentence
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- C program to swap adjacent characters of a String
- Java Program to Get the First and the Last Element of a Linked List
- Java Program to Reverse a Sentence Using Recursion
- Swap all occurrences of two characters to get lexicographically smallest string
- Swap characters in a String
- How to find the first and last character of a string in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.