Advertisement
m1o2

C StrStr findStr m1o2 biggest string in string

Apr 8th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define STR_MAX_LENGTH 50
  5.  
  6.  
  7. void findStr( const char *first, const char *second, char *buffer ){
  8.  
  9.     int startIndex = -1;
  10.     int length = -1;
  11.  
  12.     int firstIndex, secondIndex;
  13.  
  14.     for( firstIndex = 0; *( first + firstIndex ); ++firstIndex ){
  15.  
  16.         for( secondIndex = 0; *( second + secondIndex ); ++secondIndex ){
  17.  
  18.             if( *( first + firstIndex ) == *( second + secondIndex ) ){
  19.  
  20.                 int currentFirstIndex = firstIndex, currentSecondIndex = secondIndex;
  21.                 int currentLength = 0;
  22.                
  23.                 for( ; *( first + currentFirstIndex ) && *( second + currentSecondIndex ) && *(first + currentFirstIndex ) == *( second + currentSecondIndex ); ++currentFirstIndex, ++currentSecondIndex, ++currentLength )
  24.                     ;
  25.  
  26.                 if( currentLength > length ){
  27.  
  28.                     length = currentLength;
  29.                     startIndex = firstIndex;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.  
  35.     if( length > 0 ){
  36.  
  37.         int index;
  38.         for( index = 0; length--; ++index )
  39.             buffer[ index ] = *( first + startIndex++ );
  40.  
  41.         buffer[index] = '\0';
  42.     }
  43. }
  44.  
  45. int main( int argsLength, char *argv[] ){
  46.  
  47.     char first[] = "abbcccddddfffffddddccccbbaa", second[] = "fffff", buffer[STR_MAX_LENGTH] = {0};
  48.  
  49.     findStr( first, second, buffer );
  50.  
  51.     printf("\n\nfirst = %s, second = %s, result = %s\n\n", first, second, buffer );
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement