Reverse every word of the string except the first and the last character
Given 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.
Examples:
Input: str = “geeks for geeks”
Output: gkees for gkees
Input: str = “this is a string”
Output: this 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:
C++
#include <bits/stdc++.h>
using namespace std;
string reverseWord(string str)
{
int len = str.length();
int i = 1;
int j = str.length() - 2;
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
return str;
}
void reverseWords( char str[])
{
char * tok = strtok (str, " " );
while (tok != NULL) {
cout << reverseWord(tok) << " " ;
tok = strtok (NULL, " " );
}
}
int main()
{
char str[] = "geeks for geeks" ;
reverseWords(str);
return 0;
}
|
Java
class GFG
{
static String reverseWord(String str)
{
int len = str.length();
int i = 1 ;
int j = str.length() - 2 ;
char [] strchar = str.toCharArray();
while (i < j)
{
char temp = strchar[i];
strchar[i] = strchar[j];
strchar[j] = temp;
i++;
j--;
}
str = new String(strchar);
return str;
}
static void reverseWords(String str)
{
String[] tok = str.split( "\\s" );
for (String w:tok)
{
System.out.print(reverseWord(w) + " " );
}
}
public static void main (String[] args)
{
String str = "geeks for geeks" ;
reverseWords(str);
}
}
|
Python3
def reverseWord( Str ):
i = 1
j = len ( Str ) - 2
while (i < j):
temp = Str [i]
Str [i] = Str [j]
Str [j] = temp
i + = 1
j - = 1
return "".join( Str )
def reverseWords( Str ):
Str = Str .split()
for i in Str :
j = [h for h in i]
print (reverseWord(j), end = " " )
Str = "geeks for geeks"
reverseWords( Str )
|
C#
using System;
class GFG
{
static String reverseWord(String str)
{
int len = str.Length;
int i = 1;
int j = str.Length - 2;
char [] strchar = str.ToCharArray();
while (i < j)
{
char temp = strchar[i];
strchar[i] = strchar[j];
strchar[j] = temp;
i++;
j--;
}
str = new String(strchar);
return str;
}
static void reverseWords(String str)
{
String[] tok = str.Split( ' ' );
foreach (String w in tok)
{
Console.Write(reverseWord(w) + " " );
}
}
public static void Main (String[] args)
{
String str = "geeks for geeks" ;
reverseWords(str);
}
}
|
Javascript
<script>
function reverseWord(str) {
var len = str.length;
var i = 1;
var j = str.length - 2;
var strchar = str.split( "" );
while (i < j) {
var temp = strchar[i];
strchar[i] = strchar[j];
strchar[j] = temp;
i++;
j--;
}
str = strchar.join( "" );
return str;
}
function reverseWords(str) {
var tok = str.split( " " );
for (const w of tok) {
document.write(reverseWord(w) + " " );
}
}
var str = "geeks for geeks" ;
reverseWords(str);
</script>
|
Time complexity: O(N) where N is the length of the given string
Auxiliary space: O(N)
Last Updated :
19 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...