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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jul 10, 2012 // Taken From: http://programmingnotes.org/ // File: palindrome.cpp // Description: Palindrome number checker // ============================================================================ #include <iostream> #include <cctype> #include <cstring> #include <cstdlib> using namespace std; // function prototypes void Reverse(char arry[]); bool IsArryANum(char arry[]); // constant, which is the exit value const char* EXIT_VALUE = "0"; int main() { // declare variables char arry[80]; char arryReversed[80]; do{// get data from user using do/while loop cout<<"\nEnter a positive integer, or ("<<EXIT_VALUE<<") to exit: "; cin >> arry; if(atoi(arry) < 0) // check for negative numbers { cout <<"\n*** Error: "<<arry<<" must be greater than zero\n"; } else if(!IsArryANum(arry)) // check for any letters { cout <<"\n*** Error: "<<arry<<" is not an integer\n"; } else if(strcmp(EXIT_VALUE, arry) == 0) // check for "exit code" { cout <<"\nExiting program...\n"; } else // if all else is good, determine if number is a palindrome { // copy the user input from the first array (arry) // into the second array (arryReversed) strcpy(arryReversed, arry); // function call to reverse the contents inside the // "arryReversed" array to check for similarity Reverse(arryReversed); cout <<endl<<arry; // use strcmp to determine if the two arrays are the same if(strcmp(arryReversed, arry) == 0) { cout <<" is a Palindrome..\n"; } else { cout <<" is NOT a Palindrome!\n"; } } }while(strcmp(EXIT_VALUE, arry) != 0); // keep going until user enters the exit value cout <<"\nBYE!\n"; return 0; }// end of main void Reverse(char arry[]) { // get the length of the current word in the array index int length = strlen(arry)-1; // increment thru each letter within the current char array index // reversing the order of the array for (int currentChar=0; currentChar < length; --length, ++currentChar) { // copy 1st letter in the array index into temp char temp = arry[currentChar]; // copy last letter in the array index into the 1st array index arry[currentChar] = arry[length]; // copy temp into last array index arry[length] = temp; } }// end of Reverse bool IsArryANum(char arry[]) { // LOOP UNTIL U REACH THE NULL CHARACTER, // AKA THE END OF THE CHAR ARRAY for(int x=0; arry[x]!='\0'; ++x) { // if the current char isnt a number, // exit the loop & return false if(!isdigit(arry[x])) { return false; } } return true; }// http://programmingnotes.org/ |
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 =======
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { // declare variable string name=""; cout << "Enter your name: "; getline(cin,name); // built in C++ function to reverse an std::string reverse(name.begin(), name.end()); cout <<"\nYour name reversed is: " <<name << endl; return 0; }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <string> using namespace std; // function prototypes void Reverse(string name); int main() { // declare variable string name=""; // get user data cout<<"Enter your name: "; getline(cin,name); cout<<"\nYour name reversed is: "; // function declaration Reverse(name); cout<<endl; return 0; }// end of main void Reverse(string name) { if(name == "") // the base case { return; } else // the recursive step { Reverse(name.substr(1)); cout<<name.at(0); } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <string> using namespace std; // function prototypes void Reverse(string name); int main() { // declare variable string name=""; // get user data cout<<"Enter your name: "; getline(cin,name); cout<<"\nYour name reversed is: "; // function declaration Reverse(name); cout<<endl; return 0; }// end of main void Reverse(string name) { // get the length of the string int nameLength = name.length()-1; while(nameLength >= 0) { cout<<name[nameLength]; --nameLength; } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
======= REVERSE A CHARACTER ARRAY =======
The following will demonstrate (4) methods which reverses a character array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> using namespace std; // function prototype void Reverse(char name[]); int main() { // declare variable char name[30]; // get user data cout<<"Enter your name: "; cin.getline(name, sizeof(name)); cout<<"\nYour name reversed is: "; // function declaration Reverse(name); cout<<endl; return 0; }// end of main void Reverse(char name[]) { if(*name=='\0') // the base case { return; } else // the recursive step { Reverse(name+1); cout<<*(name); } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream> using namespace std; // function prototypes void Reverse(char name[]); int main() { // declare variable char name[30]; // get user data cout<<"Enter your name: "; cin.getline(name, sizeof(name)); cout<<"\nYour name reversed is: "; // function declaration Reverse(name); cout<<endl; return 0; }// end of main void Reverse(char name[]) { int nameLength = 0; //get the length of array while(name[nameLength] != '\0') { ++nameLength; } //decrease the length of by 1 --nameLength; // display reversed string while(nameLength >= 0) { cout<<name[nameLength]; --nameLength; } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <iostream> #include <cstring> using namespace std; // function prototypes void Reverse(char name[]); int main() { // declare variable char name[30]; // get user data cout<<"Enter your name: "; cin.getline(name, sizeof(name)); // function declaration Reverse(name); cout<<"\nYour name reversed is: "<< name<<endl; return 0; }// end of main void Reverse(char name[]) { // private variables int nameLength = 0; char copy[30]; strcpy(copy,name); //get lenght of array while(name[nameLength] != '\0') { ++nameLength; } //decrease the length of by 1 --nameLength; // reverse the array for(int x = 0; x <= nameLength; ++x) { // rearange the order of the two arrays name[nameLength - x] = copy[x]; } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <cstring> using namespace std; // function prototypes void Reverse(char name[]); int main() { // declare variable char name[30]; // get user data cout<<"Enter your name: "; cin.getline(name, sizeof(name)); // function declaration Reverse(name); cout<<"\nYour name reversed is: "<< name<<endl; return 0; }// end of main void Reverse(char name[]) { // get the length of the current word in the array index int nameLength = strlen(name)-1; // increment thru each letter within the current char array index // reversing the order of the array for(int currentChar=0; currentChar < nameLength; --nameLength, ++currentChar) { // copy 1st letter in the array index into temp char temp = name[currentChar]; // copy last letter in the array index into the 1st array index name[currentChar] = name[nameLength]; // copy temp into last array index name[nameLength] = temp; } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <stack> using namespace std; // function prototypes void Reverse(stack<char> &name); int main() { // using the internal system stack stack<char> name; char singleChar; cout<<"Enter your name: "; while(cin.get(singleChar) && singleChar != '\n') { name.push(singleChar); } cout<<"\nYour name reversed is: "; // function declaration Reverse(name); cout<<endl; return 0; }// end of main void Reverse(stack<char> &name) { while(!name.empty()) { cout << name.top(); name.pop(); } }// http://programmingnotes.org/ |
SAMPLE OUTPUT
Enter your name: My Programming NotesYour name reversed is: setoN gnimmargorP yM