Advertisement
ben1939

תרגיל 5 סעיף א - שדרוג

Dec 11th, 2013
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    #include <stdio.h>
  2.   int my_strlen(const char str[])
  3.   {  
  4.      int counter = 0;
  5.      while (str[counter] != '\0')
  6.           counter++;
  7.    return counter;
  8.   }
  9.  
  10.  
  11.   int my_strcmp(const char str1[], const char str2[], int idx1, int idx2, int size)
  12.    {
  13.    int i;    
  14.        
  15.       for (i = 0; i <= size; i++)
  16.      if (str1[idx1 + i] != str2[idx2 + i]) return 0;
  17.  
  18.   return 1;
  19.   }
  20.  
  21.  
  22.  int strcmp_size(const char str1[], const char str2[], int size)
  23. {
  24. int idx1, idx2;
  25.  
  26.     for (idx1 = 0; idx1 < my_strlen(str1); idx1++) {
  27.     for (idx2 = 0; idx2 < my_strlen(str1); idx2++) {
  28.           if (my_strcmp(str1, str2, idx1, idx2, size) == 1) return 1;   }      }
  29.                                    
  30.      return 0;
  31.  }
  32.  
  33.  
  34. int longest_substring(const char str1[], const char str2[])
  35.  {
  36. int size;
  37.  
  38.      for (size = my_strlen(str1); size > 0; size--)
  39.          if (strcmp_size(str1, str2, size) == 1)
  40.            return size;
  41.  
  42.      return 0;
  43.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement