#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string word;
bool palindrome;
cout << "Enter a word: ";
cin >> word;
for (int x=0; x < word.length()-1; x++)
{
if (word[x] != ' ')
{
if (tolower(word[x]) != tolower(word[word.length()-(x+1)]))
{
palindrome = false;
break;
}
else { palindrome = true; }
}
}
if (palindrome) cout << "The word is a palindrome"<<endl;
else cout << "The word is not a palindrome"<<endl;
return 0;
}
The bits for a signed and unsigned int have different meaning, you could ignore it but how hard is it to make it the same time so you know for sure there won't be an error.
That's compiler determined though. Not sure how it determines asm instruction it chooses to do the comparison ( signed or unsigned ) or if it is just undefined.
Anyway C++11 introduces some ways of doing this:
1 2 3 4 5 6 7 8 9 10 11 12 13
// normal way, all standard library have type defines, as it can vary depending on the system
for( std::string::size_type x = 0; /* ... */ )
{
}
// C++11
auto x = words.length();
decltype( words.length() ) x;
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string word;
bool palindrome;
char response;
do
{
cout << "Enter a word: ";
cin >> word;
for( std::string::size_type x = 0; x < word.length()-1; x++)
{
if (word[x] != ' ')
{
if (tolower(word[x]) != tolower(word[word.length()-(x+1)]))
{
palindrome = false;
break;
}
else { palindrome = true; }
}
}
if (palindrome) cout << "The word is a palindrome"<<endl;
else cout << "The word is not a palindrome"<<endl;
cout << "Would you like to enter another word? [y/n]";
cin >> response;
response = tolower(response);
}
while (response=='y');
return 0;
}
Those were just some different ways of declaring the type as in some cases you can have something like this:
1 2 3 4 5 6 7 8 9 10
std::vector< nameofsomelongclass > vect;
for( std::vector< nameofsomelongclass >::iterator it = vect.begin(); ... )
{
}
for( auto it = vect.begin(); ... )
{
}
Can probably see which is easier.
Also just to put some thought into your algorithm, if the words are palindromes then they are symmetric yah ? So would you need to test the entire word or just half of it ? Not to confuse you though, you are currently testing the whole word twice essentially.