Given a string str consisting of a sentence, the task is to reverse every word of the sentence except the first and last character of the words.
Input: str = “geeks for geeks”
Output: gkees for gkees
Input: str = “this is a string”
Output: tihs is a snirtg
Approach: Break the string into words using strtok(), now for every word take two pointers i and j pointing to the second and the second last character of the string respectively. Swap these characters then increment i and decrement j. Repeat these steps while i < j.
Below is the implementation of the above approach:
gkees for gkees
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- First substring whose reverse is a word in the string
- Capitalize the first and last character of each word in a string
- Remove the first and last character of each word in a string
- Print the first and last character of each word in a String
- Print last character of each word in a string
- Reverse all the word in a String represented as a Linked List
- Reverse each word in a linked list node
- Replace every character of string by character whose ASCII value is K times more than it
- Modify the string such that every character gets replaced with the next character in the keyboard
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Most frequent word in first String which is not present in second String
- String containing first letter of every word in a given string with spaces
- Replace every character of a string by a different character
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Length Of Last Word in a String
- Find the character in first string that is present at minimum index in second string
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.