int main()
{
Stack s1, s2;
string buf;
char buf2;
int flag = 0;
int count = 0;
while (true)
{
cout << "Enter a word or a sentence(If you want to exit,insert Ctrl+Z).\n";
cin >> buf;
int l = buf.size();
if (cin.eof()) //ctrl+z
{
break;
}
s1.Initialize(buf.size() + 1);
s2.Initialize(buf.size() + 1);
constchar * bufpt = buf.c_str();
char * buf3 = newchar[buf.size() + 1];
strcpy(buf3, buf.c_str());
//problem is here
while (count <= (l - 1))
{
char c = buf3[count];
s1.Push(c);//push the string to s1
count++;
}
count = 0;
for (int i=0;i<l/2;i++)
{
s1.Pop (buf2);
s2.Push(buf2);
if (s1.mbuffer[i] != s2.mbuffer[i])
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << "No,it is not a palindrome.\n";
}
else
{
cout << "Yes,it is a palindrome.\n";
}
delete[] buf3;
s1.RemoveAll();
s2.RemoveAll();
}
return 0;
}
Finally figured it out. in line 14 you are using "cin >> buf" to get the input. this works fine for a single word, but not for a string with spaces. I changed "cin" to std::getline(std::cin, buf); and it works fine now.
I do not understand why you are taking the hard way do do something simple, but it does work.
What you kneed to understand is that "cin" will extract what you type in up to either the first white space or '\n' whichever comes first where "getline" will extract everything up to and including the '\n'.
really appreciate it ! after i asked this problem, i edited code in other way . really thank you! i think i don't know well about input .. thankyou somuch