Thursday, October 11, 2012

Find first occurrence of a string in another

Given two strings s1 and s2, find the first index of s2 in s1 in an efficient manner.

Strategy:
 
The problem has very typical solution, starting with each character in the source string start looking if the string being searched for, exists or not. However, the implementations may differ on the way they optimize. One good approach is to first look for the starting character of the string being searched for, in the string being searched. If found, then go ahead and look for other matches character by character.

We can optimize above implementation. As strings get longer in length, one may employ various mechanisms to check for equality. One such method being checking for the first character - if that results in a match, compare the last character. If that succeeds as well, go ahead and check the second and then second last character. This way one can zero down to matches to the center of the string being searched for. This particular approach considers that some words are used again and again in a string, but the longer the strings the probability of repetition of the letter at same index zero's down.


Code:

#include<iostream>
#include<string.h>

using namespace std;

int findStringInString(string stringToSearchIn, string stringToSearchFor)
{
  int lengthIn = strlen(stringToSearchIn);
  int lengthFor = strlen(stringToSearchFor);
  
  if(lengthFor > lengthIn) {
//   System.out.println("Sub-string candidate is larger than original string");
   return -1;
  }
  
  if(lengthFor == lengthIn) {
   for(int index = 0; index < lengthIn; index++) {
    if(stringToSearchIn[index] != stringToSearchFor[index]) {
     // no match found
     return -1;
    }
   }
   return 0;
  }
  // lengthFor < lengthIn
  for(int index = 0; index < (lengthIn - lengthFor); index++) {
   if(stringToSearchIn[index] == stringToSearchFor[0]) {
    boolean found = true;
    // first char match found
    // check if the string beyond this is equal
    for(int subIndex = 0; subIndex < lengthFor; subIndex++) {
     if(stringToSearchIn[index + subIndex] != stringToSearchFor[subIndex]) {
      // no match
      found = false;
      break;
     }
    }
    if(found) {
//     System.out.println("Match found at index: " + index + " in original string.");
//     System.out.println(stringToSearchIn.substring(index, index + lengthFor));
     return index;
    }
   }
  }
  
  return -1;
 }

int main()

( string str= "abcddercd";

string str2="cd";

int a=findStringInString(str, str2);

getchar();
return 0;

}

No comments:

Post a Comment