## 353 - Pesky Palindromes

Moderator: Board moderators

ashis.csedu
New poster
Posts: 12
Joined: Sat Aug 18, 2007 11:09 pm
Location: CSE, University of Dhaka
Thanks Jan,
It's now accepted.

kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Contact:

### 353 still getting wa....pliz anyone help

is anybody there for helping.......I am crying with this problem.

Code: Select all

``````#include<iostream.h>
#include<stdio.h>
void eachletter();
void palindroms();
int check();
int palincheck();
int i,j,p;
char s[81]="";
int main()
{
int i,j,k;
freopen("353.txt","r",stdin);
while(gets(s))
{
if(s[0]=='\0')printf("\n");
else
{
p=0;
eachletter();
palindroms();
printf("The string '%s' contains %d palindromes.\n",s,p);
}
}
fclose(stdin);
return 0;
}
int check()
{
for(j=i-1;j>=0;j--)
if(s[i]==s[j])return 0;
return 1;
}
void eachletter()
{
int m;
for(i=0;s[i]!='\0';i++)
{
m=check();
if(m)
p++;
}
}
int palincheck(int ii,int jj)
{
int v1,k;
for(v1=1;v1<=(jj-i)/2;v1++)
if(s[ii+v1]!=s[jj-v1])return 0;
for(k=ii-1;k>=ii-(jj-ii);k--)
if(s[k]==s[ii])
if(palincheck(k,ii))return 0;
return 1;
}

void palindroms()
{
for(i=0;s[i];i++)
for(j=i+1;s[j];j++)
if(s[j]==s[i])
if(palincheck(i,j))p++;

}
``````
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Search the board first. And use an existing thread.

sujon
New poster
Posts: 5
Joined: Thu Aug 28, 2008 4:17 pm

### Re: 353 WA

getting continuos WA.
anybody plzzzzzzzzzzzzzzzzz help me.
my algorithom is
firs find palindrom of length 1,
then length 2 ,then 3,&
.......... n, where n is the length of the given string.
is my approach is wrong?

Code: Select all

``````#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mx 100

char p[1500][100];
int total;
int get(char s[])
{
int i;
for(i=1;i<=total;i++)
if(!strcmp(p[i],s))
return i;
return 0;

}

char*strrev(char ch[])
{
int l,i;
char t[mx];

l=strlen(ch);
l--;

for(i=0;i<=l;i++)
t[i]=ch[l-i];
t[i]='\0';
return t;
}

int main()
{

char temp[mx],substr[mx];
int i,j,l,c,n,f;

freopen("sujon.txt","r",stdin);
freopen("mine.txt","w",stdout);

int 	line=1;

while(gets(temp))
{
n=strlen(temp);
total=0;

for(l=1;l<=n;l++)///////////// length of substring
{

for(j=0;j<n-l+1;j++)
{

c=0;
for(i=j;i<=j+l-1;i++)
substr[c++]=temp[i];
substr[c]='\0';

if(!strcmp(substr,strrev(substr)))
{
f=get(substr);
if(!f)
strcpy(p[++total],substr);

}
}

}

//printf("The string '%s' contains %ld palindromes.\n",temp,total);
printf("The string '%s' contains %d palindromes.\n",temp,total);
//cout<<total<<"\n";

}

return 0;

}

``````

New poster
Posts: 4
Joined: Sat Apr 16, 2011 10:04 pm
Location: IIT, DU
Contact:

### 353 Pesky Palindromes

Getting a runtime error submitting the code below. I have no idea what is happening. Need help.

Code: Select all

``````#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int TRUE = 1;
int FALSE = 0;

char palindromeCollection[500][80];
int count = 0;

int prevoiuslyExists(char ch[])
{

for (int i = 0; i<=count; i++)
{
if (strcmp(ch,palindromeCollection[i]) == 0)
{
return TRUE;
}
}
strcpy(palindromeCollection[count], ch);
count++;

return FALSE;
}

int isPalind(char str[])
{
int len;
char str1[25];
len=strlen(str);

int i,j;
for (i=0,j=len-1; i<len; i++,j--)
str1[i]=str[j];

str1[i] = NULL;

if (strcmp(str, str1) == 0)
{
if (prevoiuslyExists(str) == TRUE)
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
}

int main()
{
char input[80];

while (gets(input)!=NULL)
{
int palindCount = 0;

for (int i = 0; i<500; i++)
{
palindromeCollection[i][0] = NULL;
}

int length = strlen(input);

for (int i = 0; i<length; i++)
{
for (int j = i; j<length; j++)
{
char temp[80];
int a = 0;
for (int k = i; k<=j; k++, a++)
{
temp[a] = input[k];
}
temp[a] = NULL;

if (isPalind(temp) == TRUE)
{
palindCount++;
}
}
}

printf("The string '%s' contains %d palindromes.\n", input, palindCount);
count = 0;
}

return 0;
}

``````
You are the one, who can make a difference...

newkid
Learning poster
Posts: 73
Joined: Fri Dec 12, 2008 3:06 am

### Re: 353 Pesky Palindromes

I see several things that could led to an RE

Make the collection word size 81 as you need to give space for the NULl character as well.

Code: Select all

``char palindromeCollection[500][81]``

Code: Select all

``````int isPalind(char str[])
{
int len;
char str1[81];  // Change this to 81 as well.
len=strlen(str);

int i,j;
for (i=0,j=len-1; i<j; i++,j--)  // You don't need to loop through the whole length
str1[i]=str[j];

str1[len] = NULL;  // Since you already know the length

if (strcmp(str, str1) == 0)
{
if (prevoiuslyExists(str) == TRUE)
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
}``````
In main function

Code: Select all

``````char input[81]; // change this to 81 as well
..
..
char temp[81];  // change this to 81
``````
Hope this helps
hmm..

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### 353 --- Pesky Palindromes---WA

here is my code:

Code: Select all

``````#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}

return p;
}
int main()
{
char in[100];
//while(scanf("%s",in)!=EOF)
while(gets(in))
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && isalpha(ch[i]))
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}

printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 353 --- Nice Tests...

Try using scanf instead of gets.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### Re: 353 --- Nice Tests...

Boss
Still W A............

Code: Select all

``````#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}

return p;
}
int main()
{
char in[100];
while(scanf("%s",in)!=EOF)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && isalpha(ch[i]))
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}

printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 353 --- Nice Tests...

For input 1a1 my AC code returns 3 palindromes.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### Re: 353 --- Nice Tests...

Boss,
Still W A............plz help

Code: Select all

``````#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}

return p;
}
int main()
{
char in[100];
while(scanf("%s",in)!=EOF)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && ( isalpha(ch[i]) || isalnum(ch[i]) ) )
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}

printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 353 --- Nice Tests...

For input [)[ my AC code returns 3 palindromes.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### Re: 353 --- Nice Tests...

Guru
I can not understand why I am getting WA? Please, something.....................

Code: Select all

``````    #include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j] )
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}

return p;
}
int main()
{
char in[100];
//freopen("in-353.txt","r",stdin);
//freopen("innn-353.txt","w",stdout);
while(cin>>in)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1){
printf ("The string '%s' contains 1 palindromes.\n", in);
}
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!=' ' )
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!=' ')
{
pn++;
ch[j]=' ';
}
}
}
}
printf ("The string '%s' contains %d palindromes.\n", in, pn);
}
}
return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 353 --- Nice Tests...

abcab 3 palindromes.
Check input and AC output for thousands of problems on uDebug!

Yousuf
New poster
Posts: 13
Joined: Thu Jun 09, 2011 8:22 am

### Re: 353 --- Nice Tests...

AC ..
Last edited by Yousuf on Thu Aug 02, 2012 9:03 am, edited 1 time in total.