XemMeg
06-30-2010, 12:45 AM
Hello!
I've been working on creating a program which detects if the string the user entered is a palindrome.
For some reason, my program doesn't seem to be looping. My methodology was to compare the first letter to the last letter, 2nd to first to the 2nd to last, etc.
If they all matched up, then it's a palindrome.
Here is the code below:
#include <iostream>
#include <string>
using namespace std;
void main() {
char phrase[85];
bool palindrome;
int length;
int count = 0;
cout << "Enter a phrase: ";
cin.getline (phrase, 84);
length = strlen (phrase);
for (count = 0; count != length / 2.0; count++) {
int end = length - count;
if (phrase[count] == phrase [end]) {
cout << "This is a palindrome!";
}
else {
break;
}
}
}
Iplayed around with cout to figure out what was going wrong...The program can cout phrase [count], but not phrase [end]. I'm puzzled as to why this is. I would appreciate someone's suggestions or help.
I've been working on creating a program which detects if the string the user entered is a palindrome.
For some reason, my program doesn't seem to be looping. My methodology was to compare the first letter to the last letter, 2nd to first to the 2nd to last, etc.
If they all matched up, then it's a palindrome.
Here is the code below:
#include <iostream>
#include <string>
using namespace std;
void main() {
char phrase[85];
bool palindrome;
int length;
int count = 0;
cout << "Enter a phrase: ";
cin.getline (phrase, 84);
length = strlen (phrase);
for (count = 0; count != length / 2.0; count++) {
int end = length - count;
if (phrase[count] == phrase [end]) {
cout << "This is a palindrome!";
}
else {
break;
}
}
}
Iplayed around with cout to figure out what was going wrong...The program can cout phrase [count], but not phrase [end]. I'm puzzled as to why this is. I would appreciate someone's suggestions or help.