Advertisement
bgdragoslav

LongestWord

May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define nombredesmots 5
  5. int main()
  6. {
  7.     char* a[10];
  8.     char* b[10];
  9.     int i,j,k[10],index=0,compteur[10];
  10.     //on the next 10 rows the input will be initiated as two  arrays with words
  11.     for(i=0;i<5;i++)
  12.     {
  13.         a[i]=(char*) malloc(10);
  14.         scanf("%s",a[i]);
  15.     }
  16.     for(i=0;i<5;i++)
  17.     {
  18.         b[i]=(char*) malloc(10);
  19.         scanf("%s",b[i]);
  20.     }
  21.     /* starting to compare every word with every to establish
  22.     which are identical and take the position from the first array and lenght of the word as well
  23.     */
  24.     for(i=0;i<5;i++)
  25.     {
  26.         for(j=0;j<5;j++)
  27.         {
  28.             if(strcmp(a[i],b[j])==0)
  29.             {  
  30.             k[index]=i;
  31.             compteur[index]=strlen(a[i]);
  32.             index++;
  33.             }
  34.         }
  35.     }
  36.     int max,position;
  37.     //check if the counter array is empty and check which word of the equals has the biggest lenght and print it on the console
  38.     if(index==-1);
  39.     else  
  40.     {
  41.         max=compteur[0];
  42.         for(i=1;i<index;i++)
  43.         {
  44.             if(max<compteur[i]) {max=compteur[i];position=i;}
  45.         }
  46.         printf("%s",a[k[position]]);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement