Elavenil likes strings a lot but she likes palindromic strings even more. Today she found an old string 's' in his garage. The string is so old that some of its characters have faded and are unidentifiable now. Faded characters in the string are represented by '.' whereas other characters are lower case Latin alphabets i.e ['a'-'z'].Elavenil being the palindrome lover decided to construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case Latin alphabet. Can you please help her complete the task?
#include<stdio.h>
#include<string.h>
int main()
{
int t;
char pali[500];
scanf("%d",&t);
while(t>0)
{
int c=0;
int i,j;
scanf("%s", pali);
for (i=0,j=strlen(pali);i<=strlen(pali)/2;i++,j--)
{
if(pali[i]=='.' && pali[j-1]=='.')
{
pali[i] ='a';
pali[j-1] ='a';
}
else if(pali[i]=='.')
pali[i]=pali [j-1];
else if(i==strlen(pali)/2 && pali[i]=='.')
pali[i]='1';
else if(pali[i]!=pali[j-1] && pali[i]!='.' && pali[j-1]!='.')
{
c++;
printf("-1");
break;
}
else
pali[j-1]=pali[i];
}
if(c==0)
printf("%s",pali);
printf("\n");
t--;
}
return 0;
}
Comments
Post a Comment