Caracteres X inversos del medio

Dada una string str y un entero X . La tarea es invertir los caracteres X centrales de la string dada y luego imprimir la string modificada. Tenga en cuenta que len(str) – X siempre es par.
Ejemplos: 
 

Entrada: str = “geeksforgeeks”, X = 3 
Salida: geeksrofgeeks Los 
tres caracteres del medio son “geeks for geeks” 
Por lo tanto, la string resultante es “geeks rof geeks”
Entrada: str = “acknowledgement”, X = 7 
Salida: acknegdelwoment 
 

Acercarse: 
 

  • Dado que no necesitamos invertir el primer y el último carácter. Encuentre el número de caracteres que no necesitamos invertir al principio y al final, es decir , n = len(str) – X / 2 .
  • Imprime los primeros n caracteres tal cual.
  • Luego imprima los caracteres x del medio comenzando desde n + 1 en orden inverso.
  • Finalmente, los últimos n caracteres tal cual.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to reverse the middle x characters in a string
void reverse(string str, int x)
{
    // Find the position from where
    // the characters have to be reversed
    int n = (str.length() - x) / 2;
 
    // Print the first n characters
    for (int i = 0; i < n; i++)
        cout << str[i];
 
    // Print the middle x characters in reverse
    for (int i = n + x - 1; i >= n; i--)
        cout << str[i];
 
    // Print the last n characters
    for (int i = n + x; i < str.length(); i++)
        cout << str[i];
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int x = 3;
    reverse(str, x);
    return 0;
}

Java

// Java implementation of the above approach
class GfG
{
 
    // Function to reverse the middle x
    // characters in a string
    static void reverse(String str, int x)
    {
        // Find the position from where
        // the characters have to be reversed
        int n = (str.length() - x) / 2;
     
        // Print the first n characters
        for (int i = 0; i < n; i++)
            System.out.print(str.charAt(i));
     
        // Print the middle x characters in reverse
        for (int i = n + x - 1; i >= n; i--)
            System.out.print(str.charAt(i));
     
        // Print the last n characters
        for (int i = n + x; i < str.length(); i++)
            System.out.print(str.charAt(i));
    }
 
    // Drived code
    public static void main(String []args)
    {
        String str = "geeksforgeeks";
        int x = 3;
        reverse(str, x);
    }
}
 
// This code is contributed by Rituraj Jain

Python3

# Python3 implementation of the approach
 
# Function to reverse the middle x characters in a string
def reverse(str1, x):
 
    # Find the position from where
    # the characters have to be reversed
    n = (len(str1) - x) // 2
 
    # Print the first n characters
    for i in range(n):
        print(str1[i], end="")
 
    # Print the middle x characters in reverse
    for i in range(n + x - 1, n - 1, -1):
        print(str1[i], end="")
 
    # Print the last n characters
    for i in range(n + x, len(str1)):
        print(str1[i], end="")
 
 
# Driver code
str1 = "geeksforgeeks"
x = 3
reverse(str1, x)
 
# This code is contributed by mohit kumar 29.

C#

// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to reverse the middle x
// characters in a string
static void reverse(string str, int x)
{
    // Find the position from where
    // the characters have to be reversed
    int n = (str.Length - x) / 2;
 
    // Print the first n characters
    for (int i = 0; i < n; i++)
        Console.Write(str[i]);
 
    // Print the middle x characters in reverse
    for (int i = n + x - 1; i >= n; i--)
        Console.Write(str[i]);
 
    // Print the last n characters
    for (int i = n + x; i < str.Length; i++)
        Console.Write(str[i]);
}
 
// Drived code
public static void Main()
{
    string str = "geeksforgeeks";
    int x = 3;
    reverse(str, x);
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP implementation of the approach
 
// Function to reverse the middle x
// characters in a string
function reverse($str, $x)
{
    // Find the position from where
    // the characters have to be reversed
    $n = (strlen($str) - $x) / 2;
 
    // Print the first n characters
    for ($i = 0; $i < $n; $i++)
        echo($str[$i]);
 
    // Print the middle x characters in reverse
    for ($i = $n + $x - 1; $i >= $n; $i--)
        echo($str[$i]);
 
    // Print the last n characters
    for ($i= $n + $x; $i < strlen($str); $i++)
        echo $str[$i];
}
 
// Driver code
$str = "geeksforgeeks";
$x = 3;
reverse($str, $x);
 
// This code is contributed by Shivi_Aggarwal
?>

Javascript

<script>
 
// JavaScript implementation of the above approach
 
    // Function to reverse the middle x
    // characters in a string
    function reverse( str , x)
    {
        // Find the position from where
        // the characters have to be reversed
        var n = (str.length - x) / 2;
 
        // Print the first n characters
        for (i = 0; i < n; i++)
            document.write(str.charAt(i));
 
        // Print the middle x characters in reverse
        for (i = n + x - 1; i >= n; i--)
            document.write(str.charAt(i));
 
        // Print the last n characters
        for (i = n + x; i < str.length; i++)
            document.write(str.charAt(i));
    }
 
    // Drived code
     
        var str = "geeksforgeeks";
        var x = 3;
        reverse(str, x);
 
// This code contributed by aashish1995
 
</script>
Producción: 

geeksrofgeeks

 

Publicación traducida automáticamente

Artículo escrito por SanjayR 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 *