Advertisement
apl-mhd

longestPalindrom

Nov 15th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.     char str[50][50];
  5.  
  6.  
  7. int wordCount(char name[100]){
  8.  
  9.     int totalWord = 1, i=1;
  10.  
  11.  
  12.     while(name[i] != '\0'){
  13.  
  14.         if( name[i] == ' '){
  15.  
  16.             totalWord++;
  17.         }
  18.  
  19.         i++;
  20.     }
  21.  
  22.  
  23.     return totalWord;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. void copyString(char name[100],  int totalWord){
  30.     int i, j=0,  m=0;
  31.  
  32.     for(i = 0; i < totalWord; i++){
  33.  
  34.  
  35.             m = 0;
  36.         while(name[j] != '\0'){
  37.  
  38.                 str[i][m] =name[j];
  39.  
  40.                 j++;
  41.                 m++;
  42.  
  43.                 if(name[j-1] == ' ')
  44.                     break;
  45.         }
  46.  
  47.  
  48.     }
  49.  
  50.   // printf("%s", str[0]);
  51.    //printf("%s", str[1]);
  52.   // printf("%s", str[2]);
  53. //    printf("%s\n", str[3]);
  54. //        printf("-------------------------------------------------\n");
  55.  
  56.  
  57.    // printf(" %c %c %c %c ", str[0][0], str[0][1], str[0][2], str[0][3]);
  58.  
  59. }
  60.  
  61. void longPalindrom(char str[50][50],int totalWord){
  62.  
  63.    // printf("%s", str[0]);
  64.  
  65.     int i,flag, j=0, max=0,size, add,m=0;
  66.     int pword[totalWord];
  67.  
  68.  
  69.     for(i=0; i< totalWord; i++){
  70.  
  71.         size = strlen(str[i]) -1;
  72.         j=0;
  73.         flag=0;
  74.         while( j<size){
  75.  
  76.             if(str[i][j] !=str[i][size]){
  77.                 flag =-1;
  78.             }
  79.             j++;
  80.             size--;
  81.         }
  82.  
  83.         if(flag ==0){
  84.  
  85.             add = i;
  86.  
  87.             pword[m] = i;
  88.             m++;
  89.  
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95.     printf("\n%s\n", str[add]);
  96.  
  97.  
  98.    for(i=0; i<totalWord; i++){
  99.  
  100.        // printf("%d", pword[i]);
  101.    }
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. int main()
  111. {
  112.  
  113.     char name[100] = "NUR, REHTOM! REDRUM!!";
  114.  
  115.     printf("enter reverse word\n\n");
  116.  
  117.    gets(name);
  118.  
  119.     int totalWord;
  120.  
  121.  
  122.    totalWord = wordCount(name); /*count total word in a string*/
  123.  
  124.  
  125.    copyString(name, totalWord); /* copy string in a 2d charcter array    'char str[50][50]' */
  126.  
  127.    longPalindrom(str, totalWord);
  128.  
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement