Tag Archives: reverse

C++ || Char Array – Palindrome Number Checker Using A Character Array, Strlen, Strcpy, & Strcmp

The following is a palindromic number checking program, which demonstrates more use of character array’s, Strlen, & Strcmp.

Want sample code for a palindrome checker which works for numbers and words? Click here.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
How to reverse a character array
Palindrome - What is it?
Strlen
Strcpy
Strcmp
Isdigit
Atoi - Convert a char array to a number
Do/While Loops
For Loops

This program first asks the user to enter a number that they wish to compare for similarity. If the number which was entered into the system is a palindrome, the program will prompt a message to the user via cout. This program determines similarity by using the strcmp function to compare two arrays together. Using a for loop, this program also demonstrates how to reverse a character array, aswell as demonstrates how to determine if the text contained in a character array is a number or not.

This program will repeatedly prompt the user for input until an “exit code” is obtained. The designated exit code in this program is the number 0 (zero). So the program will not stop asking for user input until the number 0 is entered into the program.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Enter a positive integer, or (0) to exit: L33T

*** error: "L33T" is not an integer

Enter a positive integer, or (0) to exit: -728

*** error: -728 must be greater than zero

Enter a positive integer, or (0) to exit: 1858

1858 is NOT a Palindrome!

Enter a positive integer, or (0) to exit: 7337

7337 is a Palindrome..

Enter a positive integer, or (0) to exit: 0

Exiting program...

BYE!

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