Program for Reversed String Pattern
Last Updated :
15 Sep, 2022
Given string str as the input. The task is to print the pattern as shown in the example.
Examples:
Input : str = “geeks”
Output :
g
e e
* s k
Explanation :
In the first line, print the first character in the string.
In the second line, print the next two characters in reverse order.
In the third line there are not enough characters to print, so append *s and print the characters in reverse order.
Input: str = “orange”
Output:
o
a r
e g n
Approach:
- Initialize an empty list and a variable x with the value 1.
- Traverse through the input string str and append the characters to the list.
- If the length of the list is equal to the value of x then
- Print the characters in the list in reverse order.
- Increment the value of x by 1.
- Empty the list
- If the length of the last printed character is not 0 and it is less than the value of x then append x-length(x) *s and print it in reverse order.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void reverseString(string str)
{
string r = "" ;
int x = 1;
for ( int i = 0 ; i < str.length(); i++)
{
r = r + str[i];
if (r.length() == x)
{
for ( int j = r.length() - 1; j >= 0; j--)
cout<<r.at(j) << " " ;
x += 1;
r = "" ;
cout<<endl;
}
}
if (r.length() < x && r.length() != 0)
{
for ( int k = 1; k <= x - r.length(); k++)
r = r + "*" ;
for ( int j = r.length() - 1; j >= 0; j--)
cout << r.at(j) << " " ;
}
}
int main()
{
string str = "geeks" ;
reverseString(str);
}
|
Java
class GFG
{
public static void reverseString(String str)
{
String r = "" ;
int x = 1 ;
for ( int i = 0 ; i < str.length(); i++)
{
r = r + str.charAt(i);
if (r.length() == x)
{
for ( int j = r.length() - 1 ; j >= 0 ; j--)
System.out.print(r.charAt(j) + " " );
x += 1 ;
r = "" ;
System.out.println();
}
}
if (r.length() < x && r.length() != 0 )
{
for ( int k = 1 ; k <= x - r.length(); k++)
r = r + "*" ;
for ( int j = r.length() - 1 ; j >= 0 ; j--)
System.out.print(r.charAt(j) + " " );
}
}
public static void main(String args[])
{
String str = "geeks" ;
reverseString(str);
}
}
|
Python 3
def reverseString( str ):
r = []
x = 1
for i in str :
r.append(i)
if len (r) = = x:
print ( * reversed (r))
x + = 1
r = []
if len (r)<x and len (r)! = 0 :
print ( '* ' * (x - len (r)), * reversed (r))
str = "geeks"
reverseString( str )
|
C#
using System;
class GFG
{
public static void reverseString(String str)
{
String r = "" ;
int x = 1;
for ( int i = 0 ; i < str.Length; i++)
{
r = r + str[i];
if (r.Length == x)
{
for ( int j = r.Length - 1; j >= 0; j--)
Console.Write(r[j] + " " );
x += 1;
r = "" ;
Console.WriteLine();
}
}
if (r.Length < x && r.Length != 0)
{
for ( int k = 1; k <= x - r.Length; k++)
r = r + "*" ;
for ( int j = r.Length - 1; j >= 0; j--)
Console.Write(r[j] + " " );
}
}
public static void Main(String []args)
{
String str = "geeks" ;
reverseString(str);
}
}
|
Javascript
<script>
function reverseString(str) {
var r = "" ;
var x = 1;
for ( var i = 0 ; i < str.length; i++)
{
r = r + str[i];
if (r.length == x)
{
for ( var j = r.length - 1; j >= 0; j--)
document.write( r[j] + " " );
x += 1;
r = "" ;
document.write( "<br>" );
}
}
if (r.length < x && r.length != 0)
{
for ( var k = 1; k <= x - r.length; k++)
r = r + "*" ;
for ( var j = r.length - 1; j >= 0; j--)
document.write( r[j] + " " );
}
}
var str = "geeks" ;
reverseString(str);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where n is the length of the given string
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...