Q22.Write a code to check whether the string is substring of given string or not.
String :- The string is defined as an array of characters. The difference between a character array and a string is the string ending with a special character ‘\0’.Given two strings S1 and S2, The task is to find if S1 is a substring of S2. If yes, return the index of the first occurrence, else return -1.
For Example :- Input : s1 = “Alfa”, s2 = “Alfatechlab”
Output : 0
check whether substring in a string Algorithm
START
STOP
check whether substring in a string code
#include <stdio.h>
int main()
{
char str[80]=”Alfatechlab”, search[10]=”Alfa”;
int count1 = 0, count2 = 0, i, j, flag;
while (str[count1] != ‘\0’)
count1++;
while (search[count2] != ‘\0’)
count2++;
for (i = 0; i <= count1 – count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j – i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf(“Substring Found Successfully!”);
else
printf(“Substring Not Found!”);
return 0;
}
// Returns true if s1 is substring of s2
int sub_string(string str1, string str2)
{
int len1 = str1.length();
int len2 = str2.length();
/* A loop to slide pat[] one by one */
for (int i = 0; i <= len2 – len1; i++) {
int j;
/* For current index i, check for pattern match */
for (j = 0; j < len1; j++)
if (str2[i + j] != str1[j])
break;
if (j == len1)
return 1;
}
return -1;
}
/* Driver program to test above function */
int main()
{
string str1 = “Alfa”;
string str2 = “Alfatechlab”;
int res = sub_string(str1, str2);
if (res == -1)
cout << “Substring Not Found!”;
else
cout << “Substring Found Successfully! “;
return 0;
}
class LFC {
// Returns true if s1 is substring of s2
static int sub_string(String str1, String str2)
{
int len1 = str1.length();
int len2 = str2.length();
/* A loop to slide pat[] one by one */
for (int i = 0; i <= len2 – len1; i++) {
int j;
/* For current index i, check for
pattern match */
for (j = 0; j < len1; j++)
if (str2.charAt(i + j) != str1.charAt(j))
break;
if (j == len1)
return 1;
}
return -1;
}
/* Driver program to test above function */
public static void main(String args[])
{
String str1 = “Alfa”;
String str2 = “Alfatechlab”;
int res = sub_string(str1, str2);
if (res == -1)
System.out.println(“Substring Not Found!”);
else
System.out.println(“Substring Found Successfully!”);
}
}
# Returns true if s1 is substring of s2
def sub_string(str1, str2):
len1 = len(str1)
len2 = len(str2)
# A loop to slide pat[] one by one
for i in range(len2 – len1 + 1):
# For current index i,
# check for pattern match
for j in range(len1):
if (str2[i + j] != str1[j]):
break
if j + 1 == len1 :
return i
return -1
# Driver Code
if __name__ == “__main__”:
str1 = “Alfa”
str2 = “Alfatechlab”
res = sub_string(str1, str2)
if res == -1 :
print(“Substring Not Found”)
else:
print(“Substring Found Successfully!”)
function sub_string($str1, $str2)
{
$len1 = strlen($str1);
$len2 = strlen($str2);
// A loop to slide
// pat[] one by one
for ($i = 0; $i <= $len2 – $len1; $i++)
{
$j = 0;
// For current index i,
// check for pattern match
for (; $j < $len1; $j++)
if ($str2[$i + $j] != $str1[$j])
break;
if ($j == $len1)
return $i;
}
return -1;
}
// Driver Code
$str1 = “Alfa”;
$str2 = “Alfatechlab”;
$res = sub_string($str1, $str2);
if ($res == -1)
echo “Substring Not Found!”;
else
echo “Substring Found Successfully!”;
Output
Substring Found Successfully!
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number