Advertisement
bigyan

Smallest Palindrome from a String

Apr 20th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char *strrev ( char *str ) {
  6.     int i, len=strlen(str);
  7.     char *rev = malloc(len+1);
  8.     rev[len] = '\0';
  9.     for (i=len-1; i>=0; rev[i--]=*str++);
  10.     return rev;
  11. }
  12.  
  13. int main(int argc, char *argv[]) {
  14.     if (argc!=2)
  15.         puts("Usage: small_palin <string>");
  16.     else {
  17.         char *str = argv[1];
  18.         int i=0, j, len=strlen(str);
  19.         while ( strcmp(str+i,strrev(str+i))!=0 )
  20.             ++i;
  21.         char *palin = malloc(len+1+i);
  22.         *(palin+len+1+i) = '\0';
  23.         strcpy(palin,str);
  24.         str[i]='\0';
  25.         strcpy(palin+len,strrev(str));
  26.         puts(palin);
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement