Escriba un programa Java para tomar una string de entrada e intercambiar la primera y la última palabra e invertir la palabra del medio.
Ejemplos:
Input : Hello World GFG Welcomes You Output :You semocleW GFG dlroW Hello
Acercarse:
- Primero tomamos dos strings vacías y la primera string toma la primera palabra y la segunda string toma la última palabra
- Cuando iteramos cada palabra, debemos tener cuidado con la variable que apunta a la siguiente palabra además de la última palabra.
- Ahora invertimos la String izquierda en la String dada.
- Después del proceso anterior, primero imprimimos la última palabra y el reverso de las strings izquierdas y luego la primera palabra.
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
Producción:
You semocleW GFG dlroW Hello
Complejidad de tiempo: O(N 2 ) donde N es la longitud de la string
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Bishal Kumar Dubey y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA