There's a builtin method find on string objects.
s = "Happy Birthday"
s2 = "py"
print(s.find(s2))
Python is a "batteries included language" there's code written to do most of what you want already (whatever you want).. unless this is homework :)
find returns -1 if the string cannot be found.
There's a builtin method find on string objects.
s = "Happy Birthday"
s2 = "py"
print(s.find(s2))
Python is a "batteries included language" there's code written to do most of what you want already (whatever you want).. unless this is homework :)
find returns -1 if the string cannot be found.
Ideally you would use str.find or str.index like demented hedgehog said. But you said you can't ...
Your problem is your code searches only for the first character of your search string which(the first one) is at index 2.
You are basically saying if char[0] is in s, increment index until ch == char[0] which returned 3 when I tested it but it was still wrong. Here's a way to do it.
def find_str(s, char):
index = 0
if char in s:
c = char[0]
for ch in s:
if ch == c:
if s[index:index+len(char)] == char:
return index
index += 1
return -1
print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))
It produced the following output:
3
8
-1
Videos
Hello,
There are several ways to find substrings in string, You could use substring in string you could use string.index(substring), or you could use string.find(substring) or even use regex.
I'm trying to understand if there is a significant difference between them for my use case, which is finding people names in article titles for example:
I want to check if Leonardo DiCaprio is in Leonardo DiCaprio gets called out for boarding superyacht: ‘eco-hypocrite’
What I usually do is:
def is_substring_in_string(substring: str, string: str):
#same thing as before but on the article title.
string_alphanumeric = ''.join(e for e in string if e.isalnum()).lower()
return substring in string_alphanumeric
def main():
names = ["Harrison Ford","Leonardo DiCaprio","Eddie Murphy","Bruce Willis","Will Smith"]
#removes unwanted charecters such as !@#$%^&*() etc and converts to lower case
names_alnum_lower = [''.join(e for e in x if e.isalnum()).lower() for x in names]
article_title = "Leonardo DiCaprio gets called out for boarding superyacht: eco-hypocrite"
for idx, lower_name in enumerate(names_alnum_lower):
if is_substring_in_string(lower_name,article_title):
print(f"Actor Name: '{names[idx]}' is in article title '{article_title}'")
if __name__ == '__main__':
main()Imagine there are a bunch of articles and people's names. Is this method acceptable or will I be better off using regex or something else?
if (strstr(phrase, word) != NULL) {
/* ... */
}
Note that strstr returns a pointer to the start of the word in phrase if the word word is found.
Use strstr for this.
https://cplusplus.com/reference/cstring/strstr
So, you'd write it like..
char *sent = "this is my sample example";
char *word = "sample";
char *pch = strstr(sent, word);
if(pch)
{
...
}
I am trying to find a substring in a string. It keeps coming back true when it is actually false.
It says "ba" is in s2 but it is not.