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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live