Reverse Words in a given string-Asked in Goldman Sachs Interviews

Placewit
2 min readJan 2, 2022

Problem Statement:

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

Input Format

The input consists of a single line containing a string ‘S’.

Output Format:

The output format contains a string which is the reversed version of the given string ‘S’.

Given:

i.like.this.program.very.much

Output:

much.very.program.this.like.i

Explanation of given Test Cases :

After reversing the whole string(not individual words), the input
string becomes much.very.program.this.like.i

Approach:

  • Initially, reverse the individual words of the given string one by one, for the above example, after reversing individual words the string should be “i.ekil.siht.margorp.yrev.hcum”.
  • Reverse the whole string from start to end to get the desired output “much.very.program.this.like.i” in the above example.

Code:

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--