I need some help with a homework assignment. I'm really new to C++ and I'm not sure what search terms to even enter to learn how to do what is being asked of me. If someone could provide some link and/or terms to search I would really appreciate. I'm not looking for something to copy/paste. I just don't have a clue what to do.
The first task was to create a program that determines if the integers entered by a user are a 5 digit palindrome (See code below). It has a quit message if -1 is entered, a "The number" << number1 << "is a palindrome" message, a "The number" << number1 << "is not a palindrome", and a "The number" << number1 << "is not a five digit number" message.
The second task (and this is where I'm lost) is make another palindrome program that has inputs of letters instead of numbers. Also, the input string should of any size. The program needs to be able to ignore spaces; i.e. if “a car a man a maraca” is entered, then cout should be "The statement "acaramanamaraca" is a palindrome!" If there are spaces, the program needs to recognize that the input is a statement instead of a word. Finally, if the input is -1, then a "Goodbye!" message needs to appear. I was given a hint to use getline for inputs that have spaces but we haven't covered that in class. Typing out all of the requirements makes me feel even more overwhelmed.
Please provide links/keywords so that I can learn how to accomplish this because I have no idea where to begin. Thanks
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int number1, n1, n2, n3, n4, n5;
cout << "Enter a five-digit integer (or -1 to quit): ";
cin >> number1;
n1 = number1 / 10000; //Since this type int the reminder will be discharded leaving only the most significant bit
n2 = (number1 % 10000) / 1000;
n3 = (number1 % 1000) / 100; //This line is unnecessary but I'm using to keep track of things
n4 = (number1 % 100) / 10;
n5 = (number1 % 10) / 1;
if (number1 == -1) {
cout << "Goodbye!\n";
} elseif (number1 < 10000 || number1 > 99999) {
cout << "The number " << number1 << " is not a five-digit number.\n";
} elseif (n1 == n5 && n2 == n4) {
cout << "The number " << number1 << " is a palindrome!\n";
} elseif (n1 != n5 || n2 != n4) {
cout << "The number " << number1 << " is not a palindrome.\n";
}
system("pause");
}
n1 = number1 / 10000; //Since this type int the reminder will be discharded leaving only the most significant bit
n2 = (number1 % 10000) / 1000;
n3 = (number1 % 1000) / 100; //This line is unnecessary but I'm using to keep track of things
n4 = (number1 % 100) / 10;
n5 = (number1 % 10) / 1;
you could have used loop and arrays (maybe)
PPS: there are many other ways to find palindrome (for both string and number)
If you search the forum for "palindrome" you'll find many other solutions to this problem.
To deal with spaces (and punctuation, and upper/lower case), you should create a second string that is the input string with all non-letters removed and the letters converted to upper (or lower) case. This is the string that you will test for a palindrome. So the code basically will look like:
1 2 3 4 5 6 7 8
read a line of input
if the line is "-1" then exit. // be sure to compare to the string "-1", not the number -1.
create string2 from input line // string2 is line without punctuation or spaces. All lower case
if (string2 is a palindrome)
cout << message
} else {
cout << other message
}
hint: start by getting the program working for strings that don't require removing spaces etc. In other words, get your "test for palindrome" code working first. Then you can concentrate of the code that creates string2. Be sure to print the value of string2 just before testing for a palindrome so you can be sure of what it's testing. Remove that code before turning in the assignment.