Hi, I've to made a program to check whether a string is a palindrome or not..
I've made following program.. After 2 hours of brainfucking efforts.. I'm still unable to figure out why this is not working correctly..
Thanks in advance to Helping hands
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string Mystr;
cout<<"Enter a string: ";
getline(cin,Mystr);
cout<<"\nLength is: "<<Mystr.length();
bool flag= false; // to set the status of string
// whether the string is palindrome or not
cout<<"\n___________________\n";
for (int k=Mystr.length(); k>=0; k--)
{
int temp=0;
if (Mystr[k]==Mystr[temp])
{
flag=true;
}
else
flag=false;
}
if (flag==true)
{
cout<<Mystr<<" is palindrome\n";
}
else
cout<<Mystr<<" is not palindrome\n";
cin.ignore();
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string Mystr;
cout<<"Enter a string: ";
getline(cin,Mystr);
cout<<"\nLength is: "<<Mystr.length();
bool flag= false; // to set the status of string
// whether the string is palindrome or not
cout<<"\n___________________\n";
for (int k=Mystr.length()-1; k>=0; k--)
{
cout<<"Loop "<<k<<" iteration "<<endl;
//cout<<Mystr[k]<<endl;
int temp=0;
if (Mystr[k]==Mystr[temp])
{
flag=true;
temp++;
}
else
{
flag=false;
break;
}
}
if (flag==true)
{
cout<<Mystr<<" is palindrome\n";
}
else
cout<<Mystr<<" is not palindrome\n";
//cin.ignore();
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
bool isPalindrome (string Mystr);
int main ()
{
string Mystr;
cout<<"Enter a string: ";
getline(cin,Mystr);
cout<<"\nLength is: "<<Mystr.length();
bool check;
check=isPalindrome(Mystr);
if (check)
cout<<Mystr<<" is Palindrome\n";
else
cout<<Mystr<<" is not Palindrome\n";
cin.ignore();
return 0;
}
bool IsPalindrome (string Mystr)
{
bool flag= false; // to set the status of string
// whether the string is palindrome or not
int temp=0;
cout<<"\n___________________\n";
for (int k=Mystr.length()-1; k>=0; k--)
{
cout<<"Loop "<<k<<" iteration "<<endl;
//cout<<Mystr[k]<<endl;
if (Mystr[k]==Mystr[temp])
{
flag=true;
temp++;
}
else
{
flag=false;
break;
}
}
return flag;
}