Newsgroup: comp.lang.c++
Subject: How is it that this code seems to work?
From: bobl0456@...
Date: Tue, 10 Dec 2013 13:34:25 -0800 (PST)
A student submitted this code:
bool isPalindrome(string word)
{
string::iterator forwardIt = word.begin();
string::reverse_iterator reverseIt = word.rbegin();
while (forwardIt != word.end())
{
if (*forwardIt != *reverseIt)
{
return false;
}
else
{
++forwardIt;
++reverseIt;
}
}
}
The function seems to work, even though I don't believe it should.
Specifically, I believe the code should be instead:
bool isPalindrome(string word)
{
string::iterator forwardIt = word.begin();
string::reverse_iterator reverseIt = word.rbegin();
while (forwardIt != word.end())
{
if (*forwardIt != *reverseIt)
{
return false;
}
else
{
++forwardIt;
++reverseIt;
}
}
return true; // THE CHANGE I MADE
}
The if statement that invokes this function appears to work correctly:
if (isPalindrome(substring))
Does anyone have a clue as to how this works despite the missing return statement?
Thanks in advance.
Subject: How is it that this code seems to work?
From: bobl0456@...
Date: Tue, 10 Dec 2013 13:34:25 -0800 (PST)
A student submitted this code:
bool isPalindrome(string word)
{
string::iterator forwardIt = word.begin();
string::reverse_iterator reverseIt = word.rbegin();
while (forwardIt != word.end())
{
if (*forwardIt != *reverseIt)
{
return false;
}
else
{
++forwardIt;
++reverseIt;
}
}
}
The function seems to work, even though I don't believe it should.
Specifically, I believe the code should be instead:
bool isPalindrome(string word)
{
string::iterator forwardIt = word.begin();
string::reverse_iterator reverseIt = word.rbegin();
while (forwardIt != word.end())
{
if (*forwardIt != *reverseIt)
{
return false;
}
else
{
++forwardIt;
++reverseIt;
}
}
return true; // THE CHANGE I MADE
}
The if statement that invokes this function appears to work correctly:
if (isPalindrome(substring))
Does anyone have a clue as to how this works despite the missing return statement?
Thanks in advance.
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?148139-How-is-it-that-this-code-seems-to-work&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment