Reverse the String

You are given a string 'STR'. The string contains [a-z] [A-Z] [0-9] [special characters]. You have to find the reverse of the string.

For example:

 If the given string is: STR = "abcde". You have to print the string "edcba".
follow up:
Try to solve the problem in O(1) space complexity. 
Input Format:
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run. 

Then the 'T' test cases follow.

The first and only line of each test case contains a string 'STR'.
Output Format:
For each test case, print a single line containing a single string denoting the reverse of the given string 'STR'.

The output of each test case will be printed in a separate line.

Note:

You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 ≤ T ≤ 10
1 ≤ |STR| ≤ 10 ^ 5 

Where |STR| is the length of the string STR.

Time limit: 1 sec.
CodingNinjas
author
2y

Slicing can be used to reverse a string in python.
To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0.

To reverse a string using slicing, write...read more

CodingNinjas
author
2y
Optimal Solution
  • Traverse the string and swap the first character with the last character, the second character with the second last character and so on.
  • Basically, you need to swap the i-th character w...read more
Ambika Kulkarni
1mo
First take the length of the given string. Using a for loop go from string length to 0 index in reverse manner. Then pick each chat from that index, append that in a new string.
Add answer anonymously...
VMware Software Devops Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter