Write a Java program to take an input string and exchange the first and last word and reverse the middle word.
Examples:
Input : Hello World GFG Welcomes You Output :You semocleW GFG dlroW Hello
Approach:
- First we take two empty Strings and first String take the first word and second String takes the last word
- When we iterate each word, then we must take care about the variable pointing to next word apart from the last word.
- Now we reverse the left String in the given String.
- After the above process, we first print last word and reverse of left Strings and after that the first word.
C++
// C++ Program to illustrate the solution // of above problem #include <iostream> using namespace std;
void print(string s)
{ // Taking an Empty String
string fst = "" ;
int i = 0;
for (i = 0; i < s.length();) {
// Iterating from starting index
// When we get space, loop terminates
while (s[i] != ' ' ) {
fst = fst + s[i];
i++;
}
// After getting one Word
break ;
}
// Taking an Empty String
string last = "" ;
int j = 0;
for (j = s.length() - 1; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s[j] != ' ' ) {
last = s[j] + last;
j--;
}
// After getting one Word
break ;
}
// Printing last word
cout<<last;
for ( int m = j; m >= i; m--) {
// Reversing the left characters
cout<<s[m];
}
// Printing the first word
cout<<fst;
} int main() {
string s = "Hello World GFG Welcomes You" ;
print(s);
return 0;
} //This code is contributed by vt_m. |
Java
// Java Program to illustrate the solution of above problem public class ExchangeFirstLastReverseMiddle {
static void print(String s)
{
// Taking an Empty String
String fst = "" ;
int i = 0 ;
for (i = 0 ; i < s.length();) {
// Iterating from starting index
// When we get space, loop terminates
while (s.charAt(i) != ' ' ) {
fst = fst + s.charAt(i);
i++;
}
// After getting one Word
break ;
}
// Taking an Empty String
String last = "" ;
int j = 0 ;
for (j = s.length() - 1 ; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s.charAt(j) != ' ' ) {
last = s.charAt(j) + last;
j--;
}
// After getting one Word
break ;
}
// Printing last word
System.out.print(last);
for ( int m = j; m >= i; m--) {
// Reversing the left characters
System.out.print(s.charAt(m));
}
// Printing the first word
System.out.println(fst);
}
public static void main(String[] args)
{
String s = "Hello World GFG Welcomes You" ;
print(s);
}
} |
Python3
# Python3 Program to illustrate the solution # of above problem def Print (s) :
# Taking an Empty String
fst = ""
i = 0
while (i < len (s)) :
# Iterating from starting index
# When we get space, loop terminates
while (s[i] ! = ' ' ) :
fst = fst + s[i]
i + = 1
# After getting one Word
break
# Taking an Empty String
last = ""
j = len (s) - 1
while (j > = i) :
# Iterating from last index
# When we get space, loop terminates
while (s[j] ! = ' ' ) :
last = s[j] + last
j - = 1
# After getting one Word
break
# Printing last word
print (last, end = "")
m = j
while m > = i :
# Reversing the left characters
print (s[m] , end = "")
m - = 1
# Printing the first word
print (fst, end = "")
# Driver code s = "Hello World GFG Welcomes You"
Print (s)
# This code is contributed by divyeshrabadiya07 |
C#
// C# Program to illustrate the //solution of above problem using System;
class ExchangeFirstLastReverseMiddle
{ static void print( string s)
{
// Taking an Empty String
string fst = "" ;
int i = 0;
for (i = 0; i < s.Length;) {
// Iterating from starting index
// When we get space, loop terminates
while (s[i] != ' ' )
{
fst = fst + s[i];
i++;
}
// After getting one Word
break ;
}
// Taking an Empty String
string last = "" ;
int j = 0;
for (j = s.Length - 1; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s[j] != ' ' ) {
last = s[j] + last;
j--;
}
// After getting one Word
break ;
}
// Printing last word
Console.Write(last);
for ( int m = j; m >= i; m--) {
// Reversing the left characters
Console.Write(s[m]);
}
// Printing the first word
Console.Write(fst);
}
// Driver code
public static void Main()
{
string s = "Hello World GFG Welcomes You" ;
print(s);
}
} //This code is contributed by vt_m |
Output:
You semocleW GFG dlroW Hello
Time Complexity: O(N2) where N is the length of the string
Auxiliary Space: O(1)