Array ( [0] => [1] => questions [2] => Strings [3] => Reverse-of-String ) Strings | Reverse of String | THE INQUISITIVE





Reverse of String

LEVEL:Beginner

Description

Given a string K. You have to find the reverse of given string.

Input Format

First line contains the string K.

Output Format

Print the reverse string.


Example 1:

Input
educate
Output
etacude
Example 2:

Input
never giveup
Output
puevig reven
Example 3:

Input
he is my favourite hero
Output
oreh etiruovaf ym si eh

oops

Login to see Discussion




Approach

Approach 1: Using loop

i)create an empty string answer
ii) traverse the given string in reverse order and append the characters to the answer
iii) return the answer

Time Complexity: O(n)
Space Complexity: O(n)

Approach 2: using Built in method

i)convert the given string to string builder
ii)reverse the string builder by calling builtin method reverse
iii) convert string builder to string and return it

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution