2

My function is trying to check if string contains substring without use of indexOf or regex match or any standard JS methods.

Please check this jsfiddle: https://jsfiddle.net/09x4Lpj2/

var string1 = 'applegate';
    var string2 = 'gate';

    function containsString(string1, string2){
	  var j = 0;
      var k = 0;
      var contains = 'false';
      var charArray1 = string1.split('');
      var charArray2 = string2.split('');
  
      for(var i = 0; i < charArray2.length; i++){
  	    j = i;
        if(charArray1[j++] != charArray2[k++]){
    	  contains = 'false';
        }else{
    	  contains = 'true';
        }   
      }
  
     console.log(contains);
    }
containsString(string1, string2);

This solution works only when the indexes are the same between the two strings (ex. applegate and apple). But will not work if the indexes are not the same (ex. applegate and gate). How do I manipulate the iterative values correctly so that the function returns true for both situations?

3
  • 4
    en.wikipedia.org/wiki/String_searching_algorithm Commented Oct 17, 2016 at 0:24
  • there is not need to convert string to char array. string is already char array Commented Oct 17, 2016 at 0:29
  • Two for loops, nested. Commented Oct 17, 2016 at 0:38

4 Answers 4

4

you can try this modified script of yours.

var string1 = 'applegate';
var string2 = 'gate';
var string3 = 'apple';
var string4 = 'leg';
var string5 = 'banana';

function containsString(string1, string2){
  var charArray1 = string1.split('');
  var charArray2 = string2.split('');
  var match = 0;

  // iterate from start of 1st string until length of 1st string minus length of 2nd string
  // you don't need to iterate the last part that is not longer than 2nd string since it will be false
  for(var i = 0; i < charArray1.length - charArray2.length + 1; i++){
    // reset match counter on every iteration
    match = 0;
    // iterate the 2nd string
    for(var j = 0; j < charArray2.length; j++){
      // compare corresponding char location
      if(charArray1[i+j] == charArray2[j]){
        match++;
        // just to check in console
        console.log(i, j, match, charArray1[i+j], charArray2[j]);
      } else {
        // just to check in console
        console.log(i, j, match, charArray1[i+j], charArray2[j]);
        // if not match, just skip current check
        break;
      }
      // if match already found, stop the checks, and return true
      if(match == charArray2.length){
        return true;
      }
    }
  }
  // match not found until end of iteration
  return false;
}

console.log(containsString(string1, string2));
console.log(containsString(string1, string3));
console.log(containsString(string1, string4));   
console.log(containsString(string1, string5));   // haystack does not contain needle
console.log(containsString(string4, string1));   // haystack is shorter than needle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you. This is really helpfull. Especially for environments that doesn't support indexOf method like IE < 9 etc...
1

Welcome to SO. Regex can be used.. Unless even that is also prohibited..

 function containsString(string1, string2){
    console.log(string1.match(string2) != null ? "Yes" : "No");
 }

Regex

3 Comments

thank you for the alternative solution :) however, i am trying my best to avoid regex match or any other methods so that I can develop an algorithm for this type of situation.
This will not work if string2 contains regex metacharacters like .*[]()
Ya agreed. But apparently Regex is out of question..So this cannot be THE Answer.. just a starting point. : )
0

This code has a logical problem,Only to determine whether the last character of A is equal to the corresponding character of B,Maybe the following code is what you want,add a line of code.

var string1 = 'applegate';
var string2 = 'gate';

function containsString(string1, string2){
  var j = 0;
  var k = 0;
  var contains = 'false';
  var charArray1 = string1.split('');
  var charArray2 = string2.split('');

  for(var i = 0; i < charArray2.length; i++){
    j = i;
    if(charArray1[j++] != charArray2[k++]){
      contains = 'false';
      break;
    }else{
      contains = 'true';
    }   
  }

 console.log(contains);
}

Comments

0

Check this without using any inbuilt functions

function subSearch(long,short){
var count = 0
for(i = 0;i < long.length; i++){
  for(j = 0;j < short.length; j++){
    if(short[j] != long[i + j]){
      break;
    }
    if((j+1) == short.length){
      count++;
    }
  }
}
return count;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.