isSubstring Example

#include <string>

using std::string;

// check whether word is a substring of text
bool isSubstring(string text, string word) {
  if (text.length() == 0) return false;
  if (word.length() == 0) return true;

  int tl = text.length();
  int wl = word.length();
  for (int tIndex = 0; tIndex < tl; tIndex++) {
    int wIndex = 0;
    for (int ti = tIndex; 
         ti < tl && wIndex < wl && text[ti] == word[wIndex];
         wIndex++, ti++) {
      if (wIndex == word.length() - 1)
        return true;
    }
  }

  return false;
}

// returns: true iff there exists a (possibly empty) 
//          substring A and B withing text == <A><word><B>

results matching ""

    No results matching ""