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

Was this article helpful?
👍 YesNo

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

  1. Avatar Amanda says:

    Thanks for all these methods!
    I am slightly at a loss as to why 1 has been subtracted from strlen(name) in method 7.

    int nameLength = strlen(name)-1;

    I am a newbie on C++ so can you please enlighten me on this?

    • admin admin says:

      Hello, thanks for visiting!

      To answer your question, its because arrays in C++ are zero based instead of one based. That means that the first element in an array/string starts at index 0 instead of index 1.

      To illustrate, the string “My Programming Notes” for example, is 20 characters long. If you wanted to iterate through that string, index zero would contain the letter “M”, index 1 would contain the letter “y” and so forth.

      Since the first letter in the string starts at index zero (instead of at index one), that means the last character in the string is located at the length of the string minus one.

      So in the example above, the last character in the string “My Programming Notes” is located at index 19 (since 20 is the length of the string minus one)

      That is why the statement “int nameLength = strlen(name)-1;” is present in the code.

      Hope this helps

Leave a Reply