Hello all, I am currently being driven insane by a COSC class that seems to have a large disparity between programming assignments. Prior to this assignment our programs only needed basic algebraic operators to pass the required checks. Now I have gotten completely lost and I can no longer tell what I am doing wrong here and now my code probably looks like an utter mess after having chopped it up multiple times.
My problem, because I thought this might be the simplest solution, is I cannot figure out how to use a tolower() or toupper() function to set an input to all lower or uppercase before my boolean runs its' string check. My assignment is due tomorrow and I have been at this for days, I even had 3 hours of tutor help from my school and they couldn't get it to work. The requirements here are I must modify the provided boolean function which is the first code I will post.
1 2 3 4 5 6 7 8 9 10
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2;i++) {
if (str[i] != str[length - 1 - i]) {
returnfalse;
} // if
} // for loop
returntrue;
}// isPalindrome
And here is my mess, my only victory is I have successfully gotten a while function to not cause the compiler to throw out fatal errors. I am now near meltdown point as I feel like I am running out of avenues to try.
there is no built in way to do an entire string; the C++ language folks probably thought we could manage a loop for ourselves on this one, though they are really weird about when they loop for us and when they make us DIY.
Whoa that was an insanely quick reply. This is my first class and only the 6th week of it. I have seen this code before but I only vaguely understand it and not sure where to put it. Can it just go into int main () after the cin >> inputstring or should it go into the boolean itself before the length check? *trying both now*
you can put it wherever you want to change the string's case. main is ok, or inside some other function is ok, or its own function.
its gibberish code to be sure. what it says..
create an un-named mini-function (called a lambda) indicated by the [] which calls tolower on a single character.
transform says to apply that lambda to each letter of the string, from its begin() to its end().
Begin and end are 'iterators' and all the c++ containers have them to allow you to ... iterate ... over the data one by one.
its exactly the same, by the way, as just saying
for(int i = 0; i < data.length(); i++)
data[i] = tolower(data[i]);
and honestly the above is easier to read, but the language has moved on from easy to read into the realm of hand waving so you may as well get used to it now :)
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
#include <cctype>
#include <string>
usingnamespace std;
bool isPalindrome(string str) {
int length = str.length();
for(int i = 0; i < length / 2; i++) {
str[i] = tolower(str[i]);
if(str[i] != str[length - 1 - i]) {
returnfalse;
}
}
returntrue;
}
int main() {
string Word;
cin >> Word;
if(isPalindrome(Word))
cout << Word << " is a palindrome.";
else
cout << Word << " is not a palindrome.";
return 0;
}
your problem is that you did lowercase str[i], but didn't touch str[length - 1 - i] yet
so do one thing at the time, first convert at lowercase, then check if it is a palindrome
string tolower(string str){
for(int i = 0; i < str.length(); i++)
str[i] = tolower(str[i]);
return str;
}
bool isPalindrome(string str) {
int length = str.length();
for(int i = 0; i < length / 2; i++) {
if(str[i] != str[length - 1 - i]) {
returnfalse;
}
}
returntrue;
}
int main() {
string Word;
cin >> Word;
if(isPalindrome(tolower(Word)))
cout << Word << " is a palindrome."; //note that it will print the original version, not the lowercase
else
cout << Word << " is not a palindrome.";
return 0;
}
exercise for the reader: modify the code so it checks for palindrome ignoring whitespace
to read a line including whitespace, may use
I don't recognize these strings from anywhere in the book, but the program now works flawlessly regardless of how complex of a palindrome I input. Mind you this is entry-level COSC-1436 Programming Fundamentals I. I put an absolutely absurd amount of effort into researching this and I am just going to approach my professor with the cpp file if necessary.