Q. Write a program to check whether a given string is substring of another 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'.

For Example :- Input : s1 = "find", s2 = "letsfindcourse"
Output : 5

Program to check substring in a string

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  •  
    #include <stdio.h>
    int main()
    {
    char str[80]="letsfindcourse", search[10]="find";
    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;
    }
    
    #include <bits/stdc++.h> 
    using namespace std; 
    // 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 = "find"; 
    string str2 = "letsfindcourse"; 
    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 = "find"; 
    String str2 = "letsfindcourse"; 
    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 = "find"
    str2 = "letsfindcourse"
    res = sub_string(str1, str2) 
    if res == -1 : 
    print("Substring Not Found") 
    else: 
    print("Substring Found Successfully!") 
    
    using System; 
    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[i + j] != str1[j]) 
    break; 
    if (j == len1) 
    return 1; 
    } 
    return -1; 
    } 
    /* Driver program to test above function */
    public static void Main() 
    { 
    string str1 = "find"; 
    string str2 = "letsfindcourse"; 
    int res = sub_string(str1, str2); 
    if (res == -1) 
    Console.Write("Substring Not Found"); 
    else
    Console.Write("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 = "find"; 
    $str2 = "letsfindcourse"; 
    $res = sub_string($str1, $str2); 
    if ($res == -1) 
    echo "Substring Not Found!"; 
    else
    echo "Substring Found Successfully!"; 
    
    #include <stdio.h>
    int main()
    {
    char str[80]="letsfindcourse", search[10]="find";
    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;
    }
    

    Output

    Substring Found Successfully!
    

    Recommended Programs

       Binary Search Program
       Bubble Sort Program