Invierta cada palabra de la string excepto el primer y el último carácter

Dada la string str que consiste en una oración, la tarea es invertir cada palabra de la oración excepto el primer y último carácter de las palabras.

Ejemplos:  

Entrada: str = «geeks para geeks» 
Salida: gkees para gkees

Entrada: str = «esto es una string» 
Salida: esto es un snirtg  

Enfoque: divida la string en palabras usando strtok() , ahora para cada palabra tome dos punteros, i y j apuntando al segundo y penúltimo carácter de la string respectivamente. Intercambie estos caracteres, luego incremente i y disminuya j . Repita estos pasos mientras i < j .
A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse the given word except
// the first and the last character
string reverseWord(string str)
{
    int len = str.length();
 
    // Pointer to the second character
    // of the string
    int i = 1;
 
    // Pointer to the second last
    // character of the string
    int j = str.length() - 2;
    while (i < j) {
 
        // Swap str[i] and str[j]
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++;
        j--;
    }
 
    return str;
}
 
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
void reverseWords(char str[])
{
    char* tok = strtok(str, " ");
 
    // While there are words left
    while (tok != NULL) {
 
        // Print the reversed word
        cout << reverseWord(tok) << " ";
 
        // Get the next word
        tok = strtok(NULL, " ");
    }
}
 
// Driver code
int main()
{
    char str[] = "geeks for geeks";
    reverseWords(str);
 
    return 0;
}

Java

// Java implementation of the above approach
class GFG
{
     
// Function to reverse the given word except
// the first and the last character
static String reverseWord(String str)
{
    int len = str.length();
     
    // Pointer to the second character
    // of the string
    int i = 1;
 
    // Pointer to the second last
    // character of the string
    int j = str.length() - 2;
     
    char[] strchar = str.toCharArray();
 
    while (i < j)
    {
 
        // Swap str[i] and str[j]
        char temp = strchar[i];
        strchar[i] = strchar[j];
        strchar[j] = temp;
        i++;
        j--;
    }
     
    str = new String(strchar);
    return str;
}
 
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
static void reverseWords(String str)
{
    String[] tok = str.split("\\s");
 
    // While there are words left
    for(String w:tok)
    {
 
        // Print the reversed word
        System.out.print(reverseWord(w) + " ");
    }
}
 
// Driver code
public static void main (String[] args)
{
    String str = "geeks for geeks";
    reverseWords(str);
}
}
     
// This code is contributed by AnkitRai01

Python3

# Python3 implementation of the approach
 
# Function to reverse the given word except
# the first and the last character
def reverseWord(Str):
     
    # len = len(Str)
 
    # Pointer to the second character
    # of the String
    i = 1
 
    # Pointer to the second last
    # character of the String
    j = len(Str) - 2
    while (i < j):
 
        # Swap Str[i] and Str[j]
        temp = Str[i]
        Str[i] = Str[j]
        Str[j] = temp
        i += 1
        j -= 1
 
    return "".join(Str)
 
# Function to reverse every word of the
# sentence except the first and the
# last character of the words
def reverseWords(Str):
    Str = Str.split()
 
    # While there are words left
    for i in Str:
 
        # Print the reversed word
        j = [h for h in i]
        print(reverseWord(j), end = " ")
 
# Driver code
Str= "geeks for geeks"
reverseWords(Str)
 
# This code is contributed by Mohit Kumar

C#

// C# implementation of the above approach
using System;
     
class GFG
{
     
// Function to reverse the given word except
// the first and the last character
static String reverseWord(String str)
{
    int len = str.Length;
     
    // Pointer to the second character
    // of the string
    int i = 1;
 
    // Pointer to the second last
    // character of the string
    int j = str.Length - 2;
     
    char[] strchar = str.ToCharArray();
 
    while (i < j)
    {
 
        // Swap str[i] and str[j]
        char temp = strchar[i];
        strchar[i] = strchar[j];
        strchar[j] = temp;
        i++;
        j--;
    }
    str = new String(strchar);
    return str;
}
 
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
static void reverseWords(String str)
{
    String[] tok = str.Split(' ');
 
    // While there are words left
    foreach(String w in tok)
    {
 
        // Print the reversed word
        Console.Write(reverseWord(w) + " ");
    }
}
 
// Driver code
public static void Main (String[] args)
{
    String str = "geeks for geeks";
    reverseWords(str);
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
      // JavaScript implementation of the above approach
      // Function to reverse the given word except
      // the first and the last character
      function reverseWord(str) {
        var len = str.length;
 
        // Pointer to the second character
        // of the string
        var i = 1;
 
        // Pointer to the second last
        // character of the string
        var j = str.length - 2;
 
        var strchar = str.split("");
 
        while (i < j) {
          // Swap str[i] and str[j]
          var temp = strchar[i];
          strchar[i] = strchar[j];
          strchar[j] = temp;
          i++;
          j--;
        }
        str = strchar.join("");
        return str;
      }
 
      // Function to reverse every word of the
      // sentence except the first and the
      // last character of the words
      function reverseWords(str) {
        var tok = str.split(" ");
 
        // While there are words left
        for (const w of tok) {
          // Print the reversed word
          document.write(reverseWord(w) + " ");
        }
      }
 
      // Driver code
      var str = "geeks for geeks";
      reverseWords(str);
</script>
Producción: 

gkees for gkees

 

Publicación traducida automáticamente

Artículo escrito por HamreetSingh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *