Edited:****
This program I'm trying to write is part of a homework assignment, but I'm having major trouble understanding the logic of what to do to determining a palindrome.
"Write a program, which will ask for and read in a line of text characters. The program will then determine if the line of text forms a palindrome and print whether it is or not. After printing this message, the program will ask for another line of text. The program will end when the line of text read matches the string “END” (letters in all caps).
1) When testing if the string is a palindrome, an upper and lower case character will be considered a match. Only when checking for END is there a requirement for the case to be specifically upper case.
2) The maximum number of displayable characters in a line will be 80."
This is what I have so far, just enough to read in a line of characters.
// this line is just to make sure the read in was working correctly
}
I looked at some of the forums answers, but a lot of them were using a lot of different methods. I was looking to use str commands mostly, as I figured it was easiest for me to understand. I feel dumb that I don't really understand how to read arrays/strings. Yet I keep reading the same book and answers over and over! Any help would be much appreciated.
I am currently working on a program which deals with strings and I need to read in a line of characters. However, I've been using "cin" such as below:
however, this only reads in anything up until you make a space, but i need to read in a line of characters.
Im having major trouble with this program, yet I believe I understand the key parts. We were told we can use commands like _getche/_getch but I'm quite confused about them. Also I do no there is another one called Getline, but not sure how exactly they work. Any help would be greatly appreciated.
I am unsure about the logic of how to start to check if a certain line of characters is a palindrome. If I were to enter "madam i'm adam" I need it to say It is a palindrome, and vice versa. This is for an assignment, and the teacher has taught us about arrays and strings, but I still dont understand how I would go about checking each letter to make sure it is a palindrome.
Well the string class allows you to access elements much like a char array[]. The string class also knows it's own size no matter what the user enters. Knowing this you can do some things like:
1 2 3 4 5 6 7 8 9
halfway point = string.size() / 2 ;
str1 = 0- halfway point ;
str2 = halfway point - end ;
check the first element of first half, against the last element of last half.
for ( each element in first half ; increment forwards )
for ( each element in second half ; increment backwards )
is element == element ;
note: you will have to keep in mind that odd number length of string and an even length, there will be slightly different way to calc the halfway and substrings.
Being this is an assignment, I really hope nobody goes ahead and solves it for you as this will completely defeat the purpose of you learning by doing.
Thank you gcampton, I was having trouble with the logic side of it. Now I see you would have to find the size of the string that is entered and then divide it in half because it should equally be the same both halves from lets say 0 to 4 and 4 to 8.
I updated my post above in the first post. The problem I'm having is understand which to create.
I know I should replace the string I entered above and change it to char pdrome[80]; since the max is 80 characters in the string.
I took a look at some other examples from searching palindrome on the forums, but of course everyone has a different way of approaching it. For instance I'm not sure if I should being doing an array of 80 characters, or just writing a string? Example below:
#include <iostream>
#include <string>
usingnamespace std;
void main()
{
char str[80];
// this is where I get confused . Should I do char since its reading a line/string of text or should I //do this
string str;
// reads in the line of characters
string str;
cout << "Please enter a palindrome: ";
getline (cin,str);
cout << "You entered the line: " << pdrome << "\n";
}
Always prefer string over char[] for IO. Someone can type a 100-letter palindrome. Then what? The char[80] version crashes; the string version does not.
Yea that and getline does not work with char[], it's a string class function. Oh I just noticed your declaring 2 types with same names, that isn't even going to compile. and then string str; is declared twice....
"Hellobolleh" 11 / 2 = 5.5 midway point 'b' start "Hello" end "olleh"
"Helloh" 6 / 2 = 3 midway point ' ' start "Hel" end "loh"
#include <iostream>
#include <string>
usingnamespace std;
void main()
{
string str;
getline (cin,str);
// to help with you program you should ignore the book, put it down, now go look at the string class
// library http://cplusplus.com/reference/string/string/
// you have functions in there that will be useful: size() or length() they are the same.
// substr(), you can also use all the typical operators like =, ==, +, and you can access elements using
// str[], etc, go play with them all make sample programs to test different things. You might find while
// doing other stuff you figure it out.
/* The following is gibberish
int midwaypoint = str.size() / 2;
if str [ start ] == str [ end ] ;
if str [ start +1 ] == str [ end -1 ] ;
//*/
if(*str.begin() == *str.rbegin()) will check the first and last characters. You'd need to store those in iterators an increment them, going through a loop of some kind.
All theory, by the way, but it seems like it should work.
dude....
please throw out that code, we have told you to use strings, that thing won't even compile.
use this: NEVER EVER use void main, it's non-standard
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string str;
int length;
bool determine=true;
cout << "Please enter string: ";
getline (cin,str);
length = str.length();
while (str.length() > 80)
{
cout << "String is too large.\n";
cout << "Only enter a string under 80 characters long: ";
getline(cin, str);
}
// I'd probaby check if i <= length/2 + 1; No real need to iterate through the whole thing.
for (unsigned i = 0; i < length; i++)
{
if (str[i] != str[length - i])
{
determine = false;
cout << "This is not a palindrome." << endl;
break;
}
}
if (determine)
cout << "Yes, it's a palindrome." << endl;
return 0;
}
you will notice that code will always come back saying this is not a palindrome even when it is, that is because there's a slight miscalculation in the line testing the characters: if (str[i] != str[length - i])
so you need to fix this accordingly.
to explain a bit about array indexes.
int array[10];
if we print this out it's indexes we get this:
1 2
for ( int i=0; i<10; i++)
cout << "The current index is: " << i ;
this prints out:
0 1 2 3 4 5 6 7 8 9
this is the actual array elements, array elements always start at the "zeroth" element aka 0. (Don't ask why, it's a mathematician thing.)
But the length of this array is 10.
so if we try to access the last element using the length we get a runtime error trying to access an element that does not exist. array [ length ]; AKA, array [ 10 ];
really in the above case we would want to access the 10th element, which would be array [ 9 ];
#include <iostream>
usingnamespace std;
void main()
{
char str[80];
int length;
int i;
bool determine;
while (true)
{
cout << "Please enter a palindrome: ";
cin.getline(str,80);
if (strcmp("END",str)==0)
exit(0);
length = strlen(str)-1;
determine = true;
for (int i = 0; i <= length; i++)
{
if (str[i] != str[length - i])
{
determine = false;
break;
}
}
if (determine)
cout << "This is a palindrome.\n";
else
cout << "This is not a palindrome.\n";
}
}