Coders Packet

Reverse a String in C++

By Shivangi Singhal

The code attached below is a C++ code, which is used to reverse a string entered by the user.

The attached code, is a C++ code, to reverse a string. String reversal is a technique, which is useful when we are working with languages, that do not recognize Left Right, or Right to left languages.
The string reversal code can be written with or without using a function, The code attached below is a code written using the function. Looping can also be done to reverse a string, whereas, in the code attached below, a function is written using the swap function, which swaps individual characters from start to end of a word in a string, till the string is not ended, that is the execution gets a '\o' at the end.
In the code sample attached below, a string is taken as the input from the user, inside the main function.
The string along with its length is calculated using the string.length() function in C++, and an initialiser of the variable 'i', in the loop as 0. After that, all the three values are passed to the reverse function, and the string is passed to the reverse function, using the '&' sign, so that the changed values in the reverse function, also reflect inside the main function, when the output is printed.

 

Download Complete Code

Comments

No comments yet