We can reverse a string by performing a slicing operation on it. The following example describes how to reverse a given string.
|Enter a string: Sudhakar
Reverse string is:
Here, String slicing is to be performed on the string “Sudhakar”. Whenever we perform slicing operation on string the default index position will be ‘0’ and it is ‘-1’ in the case of reverse string. Here we are reversing the string ‘Sudhakar’ so that ‘-1’ will be the index position. Therefore the output is “rakahduS”.