problem is in input given n names and randomly chose first latter of names and make longest palindrome
like names in input are[ dinesh ,suresh ,tom ,com raku ,sapu ,toku ,dino ]the longest palindrome with useing names is this --> [[ dstctsd ]] , i am making c program of that but every time fails please help…
import java.io.*;
public class names {
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter value of n”);
int n=Integer.parseInt(br.readLine());
String[] S=new String[n];
int[] arr=new int[26];
int[] arr1=new int[26];
System.out.println(“Enter names”);
for(int i=0;i<n;i++)
{
S[i]=br.readLine();
}
for(int i=0, b=97;i<26;i++,b++)
{
arr[i]=0;
arr1[i]=b;
}
for(int i=0;i<n;i++)
{
char c=S[i].charAt(0);
int a=(int)c%97;
arr[a]+=1;
}
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(arr[j]<arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
t=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=t;
}
}
}
int flag=0,i,j;
char c = 0;
String S1=new String();
for(i=0;i<26;i++)
{
if(flag==1&&(arr[i]==1||arr[i]==0))
{
arr[i]=0;
break;
}
else if(flag==1||arr[i]%2==0)
{
for(j=0;j<arr[i]/2;j++)
{
S1=S1+(char)arr1[i];
}
arr[i]=arr[i]/2;
}
else if(flag==0&&arr[i]%2!=0)
{
flag=1;
c=(char)arr1[i];
for(j=0;j<arr[i]/2;j++)
{
S1=S1+(char)arr1[i];
}
arr[i]=arr[i]/2;
}
}
if(flag==1)
{
S1=S1+c;
}
for(;i>=0;i--)
{
for(int k=0;k<arr[i];k++)
{
S1=S1+(char)arr1[i];
}
}
System.out.println(S1);
}
}
The question is C, not Java.
#include<stdio.h>
#include<string.h>
char Pal(char , int);
int cmpr(const void *a,const void *b);
/-------------------------------------------/
char *pal(char str[],int n)
{
int ii=0;
int jj = 0;
char out[50] = {’\0’};
int k = 0;
char var_even=’\0’;
char var_odd=’\0’;
char tmp=’\0’;
while(str[ii] !=’\0’)
{
var_even=str[ii];
ii++;
var_odd=str[ii];
ii++;
if(var_even==var_odd)
{
out[jj++]=var_even;
}
else
{
ii--;
tmp=var_even;
}
}
k=strlen(out);
out[k]=tmp;
for(ii=0;ii<k;ii++)
{
out[(k+1)+ii]=out[(k-1)-ii];
}
printf(“this is longest Palindrome >> %s \n\r”,out);
}
/--------------------------------------------------/
int cmpr(const void *a,const void *b)
{
return *(char *)a - *(char *)b;
}
/-------------------------------------------------/
int main(int argc, char ** argv)
{
int i;
char str[100] = {’\0’};
for (i=1;i<argc;i++)
{
str[i-1]=argv[i][0];
}
printf(“these are the given names for Palindrome \n\r”);
for(i=1; i<argc; i++)
{
printf(">>> %s \n\v", argv[i]);
}
qsort(str,argc-1,1,cmpr);
pal(str,argc);
}