Write a program to find the substring of a given string.
Question
Write a program to find the substring of a given string.
Solution
Sure, here is a simple Python program that finds a substring in a given string:
def find_substring(main_string, sub_string):
if main_string.find(sub_string) != -1:
print("Substring found")
else:
print("Substring not found")
# Test the function
find_substring("Hello, world!", "world")
In this program, we define a function find_substring that takes two arguments: main_string and sub_string. The function uses the find method of the string object, which returns the lowest index of the substring if found in given string. If it's not found, it returns -1. So, if the result of find is not -1, we print "Substring found", otherwise we print "Substring not found".
Finally, we test the function with the string "Hello, world!" and the substring "world".
Similar Questions
Which method is used to find a substring within a string in C++?find()substr()search()locate()
How to find all occurrences of a character or substring in Strings in Java?
Given a string s, find the length of the longest substring without repeating characters.
A substring is a contiguous sequence of characters within a string. Given a string S and an integer n, write a C code to generate all substrings of length n without vowels. For example, if the given string S1 is ‘apple’ then substrings of S1 of length 3 are app, ppl, pple. And substrings of length 3 without vowels is only one and it is ppl.
Given a string A and two integer B and C.Find and return the substring of A starting from index B and ending with index C.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.