Substring Reverse Pattern Last Updated : 12 Sep, 2022 Comments Improve Suggest changes 2 Likes Like Report 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++ // C++ program to print the required pattern #include <bits/stdc++.h> 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 JavaScript <script> // Javascript program to print the required pattern // Function to print the required pattern function 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 code var s = "geeks".split(''); var n = s.length; printPattern(s, n); </script> Outputgeeks *kee* **e** Complexity Analysis: Time Complexity: O(N) since one traversal of the string is required to complete all operations hence the overall time required by the algorithm is linearAuxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant Create Quiz Comment S SanjayR Follow 2 Improve S SanjayR Follow 2 Improve Article Tags : Strings Technical Scripter C Programs DSA Reverse pattern-printing substring +3 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like