📜  子串反向模式

📅  最后修改于: 2021-05-28 02:08:16             🧑  作者: Mango

给定字符串str ,任务是打印以下示例中给出的模式:

例子:

方法:

  • 打印未修改的字符串。
  • 反转字符串并初始化i = 0j = n – 1
  • 替换s [i] =’*’s [j] =’*’并更新i = i + 1j = j – 1,然后打印修改后的字符串。
  • j – i> 1时重复上述步骤。

下面是上述方法的实现:

C++
// C++ program to print the required pattern
#include 
using namespace std;
  
// Function to print the required pattern
void printPattern(char s[], int n)
{
  
    // Print the unmodified string
    cout << s << "\n";
  
    // Reverse the string
    int i = 0, j = n - 2;
    while (i < j) {
        char c = s[i];
        s[i] = s[j];
        s[j] = c;
        i++;
        j--;
    }
  
    // Replace the first and last character by '*' then
    // second and second last character and so on
    // until the string has characters remaining
    i = 0;
    j = n - 2;
    while (j - i > 1) {
        s[i] = s[j] = '*';
        cout << s << "\n";
        i++;
        j--;
    }
}
  
// Driver code
int main()
{
    char s[] = "geeks";
    int n = sizeof(s) / sizeof(s[0]);
  
    printPattern(s, n);
    return 0;
}


Java
// Java program to print the required pattern 
class GFG
{
      
// Function to print the required pattern 
static void printPattern(char[] s, int n) 
{ 
    // Print the unmodified string 
    System.out.println(s); 
  
    // Reverse the string 
    int i = 0, j = n - 1; 
    while (i < j) 
    { 
        char c = s[i]; 
        s[i] = s[j]; 
        s[j] = c; 
        i++; 
        j--; 
    } 
  
    // Replace the first and last character 
    // by '*' then second and second last 
    // character and so on until the string
    // has characters remaining 
    i = 0; 
    j = n - 1; 
    while (j - i > 1) 
    { 
        s[i] = s[j] = '*'; 
        System.out.println(s);
        i++; 
        j--; 
    } 
} 
  
// Driver Code
public static void main(String []args)
{
    char[] s = "geeks".toCharArray(); 
    int n = s.length; 
  
    printPattern(s, n);
}
}
  
// This code is contributed by Rituraj Jain


Python3
# Python3 program to print the required pattern 
  
# Function to print the required pattern 
def printPattern(s, n): 
  
    # Print the unmodified string 
    print(''.join(s))
  
    # Reverse the string 
    i, j = 0, n - 1
      
    while i < j: 
        s[i], s[j] = s[j], s[i] 
        i += 1
        j -= 1
      
    # Replace the first and last character 
    # by '*' then second and second last 
    # character and so on until the string
    # has characters remaining 
    i, j = 0, n - 1
      
    while j - i > 1: 
        s[i], s[j] = '*', '*'
        print(''.join(s)) 
        i += 1
        j -= 1
  
# Driver code 
if __name__ == "__main__":
  
    s = "geeks"
    n = len(s)
  
    printPattern(list(s), n) 
      
# This code is contributed
# by Rituraj Jain


C#
// C# program to print the required pattern 
using System;
  
class GFG
{
      
// Function to print the required pattern 
static void printPattern(char[] s, int n) 
{ 
    // Print the unmodified string 
    Console.WriteLine(s); 
  
    // Reverse the string 
    int i = 0, j = n - 1; 
    while (i < j) 
    { 
        char c = s[i]; 
        s[i] = s[j]; 
        s[j] = c; 
        i++; 
        j--; 
    } 
  
    // Replace the first and last character 
    // by '*' then second and second last 
    // character and so on until the string
    // has characters remaining 
    i = 0; 
    j = n - 1; 
    while (j - i > 1) 
    { 
        s[i] = s[j] = '*'; 
        Console.WriteLine(s);
        i++; 
        j--; 
    } 
} 
  
// Driver Code
public static void Main(String []args)
{
    char[] s = "geeks".ToCharArray(); 
    int n = s.Length; 
  
    printPattern(s, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
geeks
*kee*
**e**

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。