sorry I'm new to this forum and new to C++. Forgive me if i broke any rules. I'm having a hard time figuring out what is wrong with my code. I've been looking at it for hours and at this point I"m too tired to notice anything. I think I need fresh eyes. I'm pretty sure its a simple mistake. The program runs but calls anything a Palindrome! Help!@
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<cctype.h>
int main()
{char str[80];
int strlength;
int j=0;
int i=0;
int front=0,back; int flag=1;
cout<<"Enter a word\n";
cin>>str;
cin.get();
//Get the length of string str
strlength =strlen(str);
cout<<"You enter the word "<<str<<" with the length of "<<strlength<<endl;
cin.get();
//Convert all the characters in the string str to uppercase
for(int i = 0; i < strlength; i++)
if(str[i])
str[i]=toupper(str[i]);
//Remove all the special characters except letters a - z
for(int i = 0; i < strlength; i++)
{if(isalpha(str[i]))
{str[j] = str[i];
j++;}
str[j]= '\0';
//front index is pointed to the first character in the string str
front = 0;
//back index points to the last character in the string str
back = 79;
//Compare the first character with the last character. If they're are the same,
//compare the next character.
for(i=0; i< j/2 ;i++)
{if(str[front] != str[back])
{ flag = 0;
break;
}
front= front +1;
back=back -1;
}
if(flag = 0)
cout<<"It is not a palindrome"<<endl;
else
cout<<"It's a palindrome"<<endl;
return 0;
}
}
I think you totally screwed up your braces, unfortunately in a way that was valid syntax, so it doesn't show up as a compiler error, just a logic one when you run your code. Right now you're outputting palindrome / not inside your loop that traverses the string, which I assume is not right. Changed the formatting for clarity, take another look at it:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<cctype>
int main() {
char str[80];
int strlength;
int j=0;
int i=0;
int front=0,back; int flag=1;
cout<<"Enter a word\n";
cin>>str;
cin.get();
//Get the length of string str
strlength =strlen(str);
cout<<"You enter the word "<<str<<" with the length of "<<strlength<<endl;
cin.get();
//Convert all the characters in the string str to uppercase
for (int i = 0; i < strlength; i++)
if(str[i])
str[i]=toupper(str[i]);
//Remove all the special characters except letters a - z
for (int i = 0; i < strlength; i++) {
if(isalpha(str[i])) {
str[j] = str[i];
j++;
}
str[j] = '\0';
//front index is pointed to the first character in the string str
front = 0;
//back index points to the last character in the string str
back = 79;
//Compare the first character with the last character. If they're are the same,
//compare the next character.
for(i=0; i< j/2 ;i++) {
if(str[front] != str[back]) {
flag = 0;
break;
}
front = front +1;
back = back -1;
}
if(flag = 0)
cout<<"It is not a palindrome"<<endl;
else
cout<<"It's a palindrome"<<endl;
return 0;
}
}
Consider viewing a style guide, I think you're doing your block formatting in a way that's messing you up.