Tuesday, 27 December 2016

C program: Making a string palindrome

Develop a c code for the following function( using Pointer-function).
char Letter(char* [],n)
{
…………….
……………
…………….
}
Sample Input
4
abc
abcba
abcd
cba
Sample Output
2
0
4

2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void letter(char *s[], int n)
{
int i,l1,j,k,diff,c=0;
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
l1=strlen(s[i])-1;
for(j=0;j<l1;j++,l1--)
{
diff=*(*(s+i)+j)-*(*(s+i)+l1);
c=c+abs(diff);
*(*(s+i)+l1)=*(*(s+i)+l1)+diff;
}
printf("%d\n",c);
puts(s[i]);
}
}
main()
{
  int T,i;
  char *s[10],s1[10];
  printf("enter the number of test cases");
  scanf("%d",&T);
  for(i=0;i<T;i++)
  s[i]=(char *)malloc(sizeof(s1));
  letter(s,T);
}

No comments:

Post a Comment