Check if a string is substring of another

Here given code implementation process.

//C Program 
//Check if a string is substring of another
#include <stdio.h>


void substring(char str1[],char str2[],int s1,int s2)
{
  
  //Used to hold substring index
  int result = -1;

  int j = 1;

  for (int i = 0; i < s1-s2 && result==-1; ++i)
  {
    
    if(str1[i]==str2[0])
    {
  
      for (j=1 ; j < s2 ; ++j)
      {
        if(str1[i+j]!=str2[j])
        {
          break;
        }
      }
      if(j >= s2)
      {
        result=i;
      }
    
    }
  }
  printf("%s \n",str1 );

  if(result!=-1)
  {
    printf("Substring [%s] exists in location  %d\n",str2,result );
  }
  else
  {
    printf("Substring [%s] are not exists \n",str2 );
  }

}
int main()
{
    
  //define text characters
  char str1[]="Areyoureadytocode";
  char str2[]="ready";
  char str3[]="yours";
  char str4[]="to";
  //Get the size of character arrays
  int s1=sizeof(str1)/sizeof(str1[0])-1;
  int s2=sizeof(str2)/sizeof(str2[0])-1;


  substring(str1,str2,s1,s2);

  //Get the size of character arrays
  s2=sizeof(str3)/sizeof(str3[0])-1;

  substring(str1,str3,s1,s2);

  //Get the size of character arrays
  s2=sizeof(str4)/sizeof(str4[0])-1;

  substring(str1,str4,s1,s2);
  return 0;
}

Output

Areyoureadytocode
Substring [ready] exists in location  6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location  11
// Java program
// Check if a string is substring of another

public class MyString {

  public void substring(String str1, String str2) {
    
    //Get the length of given string
    int s1 = str1.length();
    
    int s2 = str2.length();

    //Used to hold substring index
    int result = -1;

    int j = 1;

    for (int i = 0; i < s1 - s2 && result == -1; ++i) 
    {
      if (str1.charAt(i) == str2.charAt(0)) 
      {
        for (j = 1; j < s2; ++j) 
        {
          if (str1.charAt(i + j) != str2.charAt(j)) 
          {
            break;
          }
        }
        if (j >= s2) 
        {
          //when substring exist
          result = i;
        }
      }
    }
    System.out.print(str1+"\n");
    
    if (result != -1) 
    {
      System.out.print("Substring ["+str2+"] exists in location "+result+"\n");
    }
    else 
    {
      System.out.print("Substring ["+str2+"] are not exists \n");
    }
  }
  public static void main(String[] args) {

    MyString obj = new MyString();
    
    //Given string
    String text = "Areyoureadytocode";

    //Test Case
    obj.substring(text,"ready");
    obj.substring(text,"yours");
    obj.substring(text,"to");
  }
}

Output

Areyoureadytocode
Substring [ready] exists in location  6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location  11
// C++ program
// Check if a string is substring of another
#include<iostream>

using namespace std;
class MyString {
	public:
		void substring(string str1, string str2) {
			//Get the length of given string
			int s1 = str1.size();
			int s2 = str2.size();
			//Used to hold substring index
			int result = -1;
			int j = 1;
			for (int i = 0; i < s1 - s2 &&
				result == -1; ++i) {
				if (str1[i] == str2[0]) {
					for (j = 1; j < s2; ++j) {
						if (str1[i + j] != str2[j]) {
							break;
						}
					}
					if (j >= s2) {
						//when substring exist
						result = i;
					}
				}
			}
			cout << str1 << "\n";
			if (result != -1) {
				cout << "Substring [" << str2 << "] exists in location " << result << "\n";
			} else {
				cout << "Substring [" << str2 << "] are not exists \n";
			}
		}
};
int main() {
	MyString obj =  MyString();
	//Given string
	string text = "Areyoureadytocode";
	//Test Case
	obj.substring(text, "ready");
	obj.substring(text, "yours");
	obj.substring(text, "to");
	return 0;
}

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location 11
// C# program
// Check if a string is substring of another
using System;
public class MyString {
	public void substring(String str1, String str2) {
		//Get the length of given string
		int s1 = str1.Length;
		int s2 = str2.Length;
		//Used to hold substring index
		int result = -1;
		int j = 1;
		for (int i = 0; i < s1 - s2 &&
			result == -1; ++i) {
			if (str1[i] == str2[0]) {
				for (j = 1; j < s2; ++j) {
					if (str1[i + j] != str2[j]) {
						break;
					}
				}
				if (j >= s2) {
					//when substring exist
					result = i;
				}
			}
		}
		Console.Write(str1 + "\n");
		if (result != -1) {
			Console.Write("Substring [" + str2 + "] exists in location " + result + "\n");
		} else {
			Console.Write("Substring [" + str2 + "] are not exists \n");
		}
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		//Given string
		String text = "Areyoureadytocode";
		obj.substring(text, "ready");
		obj.substring(text, "yours");
		obj.substring(text, "to");
	}
}

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location 11
<?php
// Php program
// Check if a string is substring of another
class MyString {
	public 	function substring($str1, $str2) {
		//Get the length of given string
		$s1 = strlen($str1);
		$s2 = strlen($str2);
		//Used to hold substring index
		$result = -1;
		$j = 1;
		for ($i = 0; $i < $s1 - $s2 &&
			$result == -1; ++$i) {
			if ($str1[$i] == $str2[0]) {
				for ($j = 1; $j < $s2; ++$j) {
					if ($str1[$i + $j] != $str2[$j]) {
						break;
					}
				}
				if ($j >= $s2) {
					//when substring exist
					$result = $i;
				}
			}
		}
		echo($str1 ."\n");
		if ($result != -1) {
			echo("Substring [". $str2 ."] exists in location ". $result ."\n");
		} else {
			echo("Substring [". $str2 ."] are not exists \n");
		}
	}
}

function main() {
	$obj = new MyString();
	//Given string
	$text = "Areyoureadytocode";
	//Test Case
	$obj->substring($text, "ready");
	$obj->substring($text, "yours");
	$obj->substring($text, "to");

}
main();

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location 11
// Node Js program
// Check if a string is substring of another
class MyString {
	substring(str1, str2) {
		//Get the length of given string
		var s1 = str1.length;
		var s2 = str2.length;
		//Used to hold substring index
		var result = -1;
		var j = 1;
		for (var i = 0; i < s1 - s2 &&
			result == -1; ++i) {
			if (str1[i] == str2[0]) {
				for (j = 1; j < s2; ++j) {
					if (str1[i + j] != str2[j]) {
						break;
					}
				}

				if (j >= s2) {
					//when substring exist
					result = i;
				}
			}
		}

		process.stdout.write(str1 + "\n");
		if (result != -1) {
			process.stdout.write("Substring [" + str2 + "] exists in location " + result + "\n");
		} else {
			process.stdout.write("Substring [" + str2 + "] are not exists \n");
		}
	}
}

function main(args) {
	var obj = new MyString();
	//Given string
	var text = "Areyoureadytocode";
	//Test Case
	obj.substring(text, "ready");
	obj.substring(text, "yours");
	obj.substring(text, "to");
}

main();

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location 11
#  Python 3 program
#  Check if a string is substring of another
class MyString :
	def substring(self, str1, str2) :
		s1 = len(str1)
		s2 = len(str2)
		result = -1
		j = 1
		i = 0
		while (i < s1 - s2 and result == -1) :
			if (str1[i] == str2[0]) :
				j = 1
				while (j < s2) :
					if (str1[i + j] != str2[j]) :
						break
					
					j += 1
				
				if (j >= s2) :
					# when substring exist
					result = i
				
			
			i += 1
		
		print(str1)
		if (result != -1) :
			print("Substring [", str2 ,"] exists in location ", result )
		else :
			print("Substring [", str2 ,"] are not exists ")
		
	

def main() :
	obj = MyString()
	text = "Areyoureadytocode"
	obj.substring(text, "ready")
	obj.substring(text, "yours")
	obj.substring(text, "to")


if __name__ == "__main__":
	main()

Output

Areyoureadytocode
Substring [ ready ] exists in location  6
Areyoureadytocode
Substring [ yours ] are not exists
Areyoureadytocode
Substring [ to ] exists in location  11
#  Ruby program
#  Check if a string is substring of another
class MyString 
	def substring(str1, str2) 
		s1 = str1.length()
		s2 = str2.length()
		result = -1
		j = 1
		i = 0
		while (i < s1 - s2 &&
			result == -1) 
			if (str1[i] == str2[0]) 
				j = 1
				while (j < s2) 
					if (str1[i + j] != str2[j]) 
						break
					end
					j += 1
				end
				if (j >= s2) 
					# when substring exist
					result = i
				end
			end
			i += 1
		end
		print(str1 ,"\n")
		if (result != -1) 
			print("Substring [", str2 ,"] exists in location ", result ,"\n")
		else 
			print("Substring [", str2 ,"] are not exists \n")
		end
	end
end
def main() 
	obj = MyString.new()
	text = "Areyoureadytocode"
	obj.substring(text, "ready")
	obj.substring(text, "yours")
	obj.substring(text, "to")
end
main()

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists 
Areyoureadytocode
Substring [to] exists in location 11
// Scala program
// Check if a string is substring of another
class MyString {
	def substring(str1: String, str2: String): Unit = {
		var s1: Int = str1.length();
		var s2: Int = str2.length();
		var result: Int = -1;
		var j: Int = 1;
		var i: Int = 0;
		var status : Boolean = true;
		while (i < s1 - s2 &&
			result == -1) {
			if (str1(i) == str2(0)) {
				j = 1;
              	status = true;
				while (j < s2 && status==true) {
					if (str1(i + j) != str2(j)) {
						status = false;
					}
                  	else
                    {
                    	j += 1;
                    }
					
				}
				if (j >= s2) {
					//when substring exist
					result = i;
				}
			}
			i += 1;
		}
		print(str1 + "\n");

		if (result != -1) {
			print("Substring [" + str2 + "] exists in location " + result + "\n");
		} else {
			print("Substring [" + str2 + "] are not exists \n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "Areyoureadytocode";
		obj.substring(text, "ready");
		obj.substring(text, "yours");
		obj.substring(text, "to");
	}
}

Output

Areyoureadytocode
Substring [ready] exists in location 6
Areyoureadytocode
Substring [yours] are not exists
Areyoureadytocode
Substring [to] exists in location 11
// Swift program
// Check if a string is substring of another
class MyString {
	func substring(_ text1: String, _ text2: String) {
      	var str1 = Array(text1);
        var str2 = Array(text2);
		let s1: Int = str1.count;
		let s2: Int = str2.count;
		var result: Int = -1;
		var j: Int = 1;
		var i: Int = 0;
      
		while (i < s1 - s2 &&
			result == -1) {
			if (str1[i] == str2[0]) {
				j = 1;
				while (j < s2) {
					if (str1[i + j] != str2[j]) {
						break;
					}
					j += 1;
				}
				if (j >= s2) {
					//when substring exist
					result = i;
				}
			}
			i += 1;
		}
		print(text1 ,"\n", terminator: "");
		if (result != -1) {
			print("Substring [", text2 ,"] exists in location ", result ,"\n", terminator: "");
		} else {
			print("Substring [", text2 ,"] are not exists \n", terminator: "");
		}
	}
}
func main() {
	let obj: MyString = MyString();
	let text: String = "Areyoureadytocode";
	obj.substring(text, "ready");
	obj.substring(text, "yours");
	obj.substring(text, "to");
}
main();

Output

Areyoureadytocode
Substring [ ready ] exists in location  6
Areyoureadytocode
Substring [ yours ] are not exists
Areyoureadytocode
Substring [ to ] exists in location  11


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved