title: Finding the longest common word in two strings in C Language?
body: <p>I have had a problem with finding the longest common word in two strings for some time now. First I had an idea of doing it with "isspace" function, but couldn't figure out how to find a common word. Then "strcmp" came to my mind, but so far I was only able to compare two strings. I was thinking of some way to incorporate strcmp and isspace in order to find the different words and then use a temp value to find the longest one, but I couldn't think of the correct code to do so. </p>
  
  <pre><code>   #include &lt;stdio.h&gt;
  
     int strcmp(char s[],char t[]);
  
  
   void main()
   {
  
      char s[20],t[20];
     printf("Type in a string s.\n");
     gets(s);
      printf("Type in a string t.\n");
      gets( t );
     printf("The result of comparison=%d\n",strcmp(s,t));
  
      return 0;
        }
  
  
     int strcmp(char s[],char t[])
    {
     int i;
     for(i=0;s[i]==t[i];i++)
     if(s[i]=='\0')
     return( 0 );
     return(s[i]-t[i]);
     }
  </code></pre>
  
  <p>Please help me with this one. All ideas (and code) are welcomed and appreciated. Thank you in advance!</p>
  
  <p>Edit::</p>
  
  <p>I have been battling with this one for a while and I think I have the solution, however it's a very rigid method. The program has a bug, probably with array "ptrArray1", but I cannot fix it. </p>
  
  <pre><code>     #include &lt;stdio.h&gt;
       #include &lt;string.h&gt;
       #include &lt;stdlib.h&gt;
  
  
       int returnArrayOfWords (char* str4Parsing, char* arrayParsed[])
         {
  // returns the length of array
  char seps[]   = " \t\n"; // separators
  char *token = NULL;
  char *next_token = NULL;
  int i = 0;
  
  // Establish string and get the first token:
  token = strtok( str4Parsing, seps);
  
  // While there are tokens in "str4Parsing"
  while ((token != NULL))
  {
      // Get next token:
      arrayParsed[i] = token;
      //printf( " %s\n", arrayParsed[i] );//to be commented
      token = strtok( NULL, seps);
      i++;
  }
  return i;
        }
  
        void printArr(char *arr[], int n)
       {
  int i;
  for ( i = 0; i &lt; n; i++)
  {
      printf("Element %d is %s \n", i, arr[i]);
  }
     }
  
  
       void findLargestWord(char *ptrArray1[], int sizeArr1, char              *ptrArray2[], int sizeArr2)
  {
  int maxLength = 0;
  char *wordMaxLength = NULL ;
  int i = 0, j = 0;
  char *w1 = NULL, *w2 = NULL; /*pointers*/
  int currLength1 = 0, currLength2 = 0 ;
  
  //printArr(&amp;ptrArray1[0], sizeArr1);
  //printArr(&amp;ptrArray2[0], sizeArr2);
  
  for (i = 0; i &lt; sizeArr1; i++)
      {
      // to find the largest word in the array
      w1 = (ptrArray1[i]); // value of address (ptrArray1 + i)
      currLength1 = strlen(w1);
      //printf("The word from the first string is: %s and its length is : %d \n", w1, currLength1); // check point
  
      for (j = 0; j &lt; sizeArr2; j++)
          {
          w2 = (ptrArray2[j]); // value of address (ptrArray2 + j)
          currLength2 = strlen(w2);
          //printf("The word from the second string is : %s and its length is : %d \n", w2, currLength2); // check point
  
          if (strcoll(w1, w2) == 0 &amp;&amp; currLength1 == currLength2)
              // compares the strings
              {
              if (currLength2 &gt;= maxLength)
                  // in the variable maxLength -&gt; the length of the longest word
                  {
                      maxLength = currLength2;
                      wordMaxLength = w2;
                      printf("The largest word for now is : %s and its length is : %d \n", wordMaxLength, maxLength); // check point
                  }
              }
          }
      }
  printf("The largest word is: %s \n", wordMaxLength);
  printf("Its length is: %d \n", maxLength);
      }
  
  
  
       int main ()
        {
  int n = 80; /*max number of words in string*/
  char arrS1[80], arrS2[80];
  char *ptrArray1 = NULL, *ptrArray2 = NULL;
  int sizeArr1 = 0, sizeArr2 = 0;
  
  // to allocate memory:
  ptrArray1 = (char*)calloc(80, sizeof(char));
  if(ptrArray1 == NULL)
  {
      printf("Error! Memory for Pointer 1 is not allocated.");
      exit(0);
  }
  
  ptrArray2 = (char*)calloc(80, sizeof(char));
  if(ptrArray2 == NULL)
  {
      printf("Error! Memory for Pointer 2 is not allocated.");
      exit(0);
  }
  
  printf("Type your first string: ");
  fgets(arrS1, 80, stdin);
  sizeArr1 = returnArrayOfWords (arrS1, &amp;ptrArray1); // sizeArr1 = number of elements in array 1
  
  printf("Type your second string: ");
  fgets(arrS2, 80, stdin);
  sizeArr2 = returnArrayOfWords (arrS2, &amp;ptrArray2); // sizeArr2 = number of elements in array 2
  
  findLargestWord(&amp;ptrArray1, sizeArr1, &amp;ptrArray2, sizeArr2);
  
  free(ptrArray1);
  free(ptrArray2);
  return 0;
            }
  </code></pre>
  
  <p>I also tried to use the latter two posted soltuions, but I have a problem with working with them as stated below. </p>
  
  <p>Any help with my code, fixing my problems with the latter solutions or coming up with new solutions is welcomed. Thank you all in advance!</p>
  
  <p>PS. I'm sorry if my code is poorly placed. I'm still not very good with using the placement.</p>