← Back to topics
Topic

z-palindromes

Martin
I don't get it, my solution seems to be working when I test it and test samples from the text work. But when I send it, it fails on 14. and 15. test samples i think. This is my code:

#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int palindrom (string a)
{
string b;
for (int i=a.size()-1;i>=0;i--)
{
a[i]=tolower(a[i]);
b+=a[i];
}
if (a==b) return 1; else return 0;
}
int main ()
{
string a,b;
int max=0,zbr=0;
getline (cin,a);
for (int i=0;i<=a.size()-1;i++)
{
b.clear();
zbr=0;
while (1==1)
{
if (i<=a.size())
{
if ((a[i]>=65 && a[i]<=122)||(a[i]>=48 && a[i]<=57))
{
goto abc;
}
}
if (zbr>0) goto pal;
goto poc;
abc:;
b+=a[i];
zbr++;
i++;
}
pal:;
if (palindrom(b)==1)
{
max++;
}
poc:;
}
cout<<max<<endl;
return 0;
}

I hope somebody who already solved this task can help me find my mistake.
matteo123
put scanf and printf instead of cin and cout because scanf and printf are faster than cin and cout
Dgleich
they are faster but he is using cin.getline so he is getting the whole line but you can do it with gets(c-string);
it will get the whole line but watch for overloads...
Dgleich
Well for instance
Char line [ 100 ] ;
int main(){
gets ( line );
printf("%s\n",line);
return 0 ;
}
Martin
Thank you so much. I used "gets" and it worked like a charm
Dgleich
No problem...:)