Javatpoint Logo
Javatpoint Logo

Substring Check

A typical issue in computer science and string manipulation is the substring check, which is string containment. Identifying whether a particular string (the substring) is a component of a larger string (the main string) is what is required. You must determine if the substring appears in the main string.

In simple words, given 2 strings, S1 and S2, you must check whether S1 contains S2 as a substring. if it is substring, return true else, false.

For example:

String S1: "growing"

String S2: "grow"

Result: The substring S2 "how" is in the main string S1.

Depending on the necessary efficiency and the programming language, this problem has various approaches. These are the three typical ways to search for a substring:

Naive Approach:

Using a nested loop is a quick and easy technique to check for a substring. The inner loop determines if the characters beginning at the current place match the characters in the substring, while the outer loop iterates through each character of the main string. The substring is present in the main string if every character matches.

Algorithm:

  • Begin by constructing a primary string and the substring you're looking for within it.
  • "The quick brown fox jumps over the lazy dog" is the main string. The word "fox."
  • Set up variables to keep track of the leading string's (n) and the substring's (m) lengths.
  • The swift brown fox jumps over the lazy hound is 44 words long. (The length of "fox") = m
  • To ensure enough characters remain to match the entire substring set up an outer loop that iterates through the characters in the main string up to (n - m) index.
  • Create an inner loop that iterates through the characters of the substring inside the outer loop.
  • Characters in the main string and the substring should be compared. Break out of the inner loop and move on to the following index in the main string if there is a mismatch.
  • If the inner loop ends successfully with no mismatches, the entire substring has been located in the main string. Get back true.
  • Return false if the outer loop ends without discovering a matching substring.

In JAVA:

Output:

Substring is present: True

A check is made using the substring method of the Java SubstringCheck class to determine whether a provided string (S2) is a substring of another string (S1). The function takes a simplistic approach by comparing the characters in both strings using two stacked loops. The inner loop advances to the following place in S1 when it discovers a mismatch. The inner loop must successfully finish without a mismatch for the method to return true, which denotes that S2 is present in S1 as a substring. When S1="growing" and S2="grow" are used to test the function in the primary method, the result is "Substring is present: true," indicating that "grow" is a substring of "growing." However, this method is ineffective for longer strings because of its temporal complexity, O(n * m).

In python:

Output:

Substring is present: True

The reasoning used in the Python code is the same as in the Java implementation. The static function substring of the SubstringCheck class uses a crude way to determine whether S2 is a substring of S1. In this scenario, "Substring is present: True" is printed because "grow" is a substring of "growing" when the method is tested with S1="growing" and S2="grow" in the main block. This implementation's temporal complexity is still O(n * m).

In C++:

Output:

Substring is present: True

This above C++ code uses the static function substring of the SubstringCheck class to determine whether S2 is a substring of S1. Checking for the substring "grow" in the main string "growing" is illustrated using this technique in the main function. The substring is printed as the output because the result is true, showing that it is present.

Built-in String Functions:

To search for substrings, most computer languages have built-in string functions.

Python:

To search for substrings in Python, use the "find()" method or the "in" keyword:

Output:

Substring is present.

The code checks whether the substring "how" is present in the main string "Hello, how are you?" using the Python keyword. Because the substring "how" is present in the main string, the statement "Substring is present." is printed when the condition if substring in main_string: evaluates to True. The else block would have been invoked and printed "Substring is not present." if the substring was absent from the main string. However, the substring is present in this instance. Therefore, the first block is run, producing the specified output.

In Java:

The "contains" method of the String class in Java can be used to perform the same substring check. The Java code for the substring check is provided below:

Output:

Substring is present.

The String class method is used in this Java code to determine whether the substring "how" is present in the main string "Hello, how are you?" The statement "mainString.contains(substring)" evaluates to true since the substring is contained in the main string, printing "Substring is present.". The message "Substring is not present." would have been displayed if the substring was missing from the main string. However, in this instance, the substring is present. Therefore, the output prints the first message.

In C++:

In C++, you can use the find method of the std::string class to perform the substring check. Here's the C++ code for the substring check:

Output:

Substring is present.

This C++ code checks whether the substring "how" is contained in the main string "Hello, how are you?" using the find function of the std::string class. If the substring cannot be discovered, the find method returns std::string::npos or the starting index of the first instance of the substring. The message "Substring is present." is printed if the returned index is not equal to std::string::npos, indicating the substring's presence. Otherwise, it prints "Substring is not present." as the output if the substring is not found. However, the substring is present, causing the initial message to be printed.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA