📜  反向中间X字符

📅  最后修改于: 2021-05-31 23:46:26             🧑  作者: Mango

给定一个字符串str和一个整数X。该任务是反转给定字符串的中间X个字符,然后打印修改后的字符串。请注意len(str)– X始终为偶数。
例子:

方法:

  • 因为我们不需要倒数第一个和最后一个字符。找到我们不需要在开头和结尾处反转的字符数,即n = len(str)– X / 2
  • 按原样打印前n个字符。
  • 然后以相反的顺序打印从n +1开始的中间x个字符。
  • 最后,按原样保留最后n个字符。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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 str1ing
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
= $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


输出:
geeksrofgeeks
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”