Daily Archives: March 1, 2012

C++ || 8 Different Ways To Reverse A String/Character Array In C++

This page will consist of 8 different ways to reverse a character array, and a string literal (std::string).

REQUIRED KNOWLEDGE FOR THE PROGRAMS

Character Arrays
String Literals
Cin.getline - Use For Char Arrays
Getline - Use For std::string
Length
Strlen
Strcpy
While Loops
For Loops
Recursion - What is it?
#include < algorithm>
#include < stack>

The methods on this page will be broken up into sections. This page will list:


(3) methods which reverses string literals (std::string)
(4) methods which reverses character arrays
(1) method which utilizes the stack to "reverse" a character sequence

Some methods listed on this page demonstrates the use of reversing a character sequence without the use of strlen/length.

======= REVERSE AN STD::STRING =======


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM

======= REVERSE A CHARACTER ARRAY =======

The following will demonstrate (4) methods which reverses a character array.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


======= REVERSE A CHARACTER SEQUENCE USING A STACK =======

The following will demonstrate (1) method which reverses a character sequence using the STL stack.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM