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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Java program to count the characters in each word in a given sentence
- Swap corner words and reverse middle characters
- C program to swap adjacent characters of a String
- Split a sentence into words in C++
- Count palindrome words in a sentence
- Remove all the palindromic words from the given sentence
- Java Program to Swap two Variables
- Java program to print Even length words in a String
- Swap characters in a String
- Possible Words using given characters in Python
- Group words with same set of characters
- Swap all occurrences of two characters to get lexicographically smallest string
- Print all valid words that are possible using Characters of Array
- Python | Toggle characters in words having same case
- K length words that can be formed from given characters without repetition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.