Substring Reverse Pattern

21 May, 2021
Given string str, the task is to print the pattern given in the examples below:
Examples:  
Input: str = “geeks” Output: geeks *kee* **e** The reverse of “geeks” is “skeeg” Replace the first and last characters with ‘*’ i.e. *kee* Replace the second and second last character in the modified string i.e. **e** And so on.
Input: str = “first” Output: first *sri* **r** 
Approach: 
Print the unmodified string.
Reverse the string and initialize i = 0 and j = n – 1.
Replace s[i] = ‘*’ and s[j] = ‘*’ and update i = i + 1 and j = j – 1 then print the modified string.
Repeat the above steps while j – i > 1.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to print the required pattern#include <bits/stdc++.h>using namespace std; // Function to print the required patternvoid 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 codeint main(){    char s[] = "geeks";    int n = sizeof(s) / sizeof(s[0]);     printPattern(s, n);    return 0;}
// Java program to print the required patternclass GFG{     // Function to print the required patternstatic 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 Codepublic static void main(String []args){    char[] s = "geeks".toCharArray();    int n = s.length;     printPattern(s, n);}} // This code is contributed by Rituraj Jain
# Python3 program to print the required pattern # Function to print the required patterndef 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 codeif __name__ == "__main__":     s = "geeks"    n = len(s)     printPattern(list(s), n)     # This code is contributed# by Rituraj Jain
// C# program to print the required patternusing System; class GFG{     // Function to print the required patternstatic 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 Codepublic static void Main(String []args){    char[] s = "geeks".ToCharArray();    int n = s.Length;     printPattern(s, n);}} // This code is contributed by 29AjayKumar
<script> // Javascript program to print the required pattern // Function to print the required patternfunction printPattern(s, n){     // Print the unmodified string    document.write( s.join('') + "<br>");     // Reverse the string    var i = 0, j = n - 1;    while (i < j) {        var 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] = '*';        document.write( s.join('') + "<br>");        i++;        j--;    }} // Driver codevar s = "geeks".split('');var n = s.length;printPattern(s, n);  </script>
geeks
*kee*
**e**
 
rituraj_jain
29AjayKumar
importantly
pattern-printing
Reverse
substring
C Programs
Strings
Technical Scripter
Strings
pattern-printing
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.