1

I receive a String. For example in here I have :

Mystring= ‘alohrefllluqoo’

I can see all the letter of 'Hello' word, in the correct sequence, in Mystring string

is it possible to use Booleans?

in that string the final output would be 'YES', cause when I remove extra letter I can see the 'Hello' word (in correct sequence) in string.

and if the sequence is not correct and the word can not be found, the output will be 'NO'

Ali Tajik
  • 39
  • 9
  • Convert both strings in lists and than see e.g. https://stackoverflow.com/questions/35964155/checking-if-list-is-a-sublist – Ocaso Protal Apr 23 '19 at 11:59
  • What result do you expect from `Mystring= 'aloehrfllluqoo'`? – sentence Apr 23 '19 at 12:16
  • Could you elaborate what you mean by "with booleans"? – Jon Clements Apr 23 '19 at 12:45
  • I think that I can made a Boolean from every letter of ‘hello’ (I don’t know how can I write this part) Make it false in the first line, and by using one loop and if, checked the any letter in sequence in first string. Finally I will checked all the FALSE or TRUE. All of them were TRUE -> output : ‘YES’ Else: ‘NO’ – Ali Tajik Apr 23 '19 at 13:15

3 Answers3

2

This is one approach.

Ex:

Mystring= 'alohrefllluqoo'
to_find = "hello"

def check_string(Mystring, to_find):
    c = 0
    for i in Mystring:
        if i == to_find[c]:
            c += 1
        if c == len(to_find):
            return "YES"
    return "NO"

print(check_string(Mystring, to_find))
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • it works properly, thanks a lot. but i have question. why you used function? as I found out you have tried to remove the importance of string range? is it correct? – Ali Tajik Apr 23 '19 at 12:55
  • `hlelo` is not in `alohrefllluqoo` – Rakesh Apr 23 '19 at 13:26
1

You can use something like this:

mystring = 'alohreflllouq'
wordtofind = "hello"

i=0
word=''

for char in mystring:
    if char == wordtofind[i]:
        word = word + char
        i+= 1
    if word == wordtofind:
        break

result = "YES" if word == wordtofind else "NO"

print(result)
Alex Ciu
  • 148
  • 1
  • 6
0

making a function, and passing your string, and the thing you search for:

def IsItHere(some_string, sub_string)
    try:
        some_string.index(sub_string)
        return 'YES'
    except:
        return 'NO'
Phisher
  • 3
  • 4
  • 1
    based on Ocaso Protal's comment, seems i've misunderstood OP's question. quoting OP, "we can see the 'Hello' word in first string" that's not a good description of the desired result. I think you could say : "we can see all the letter of 'Hello' word, in the correct sequence, in Mystring string" – Phisher Apr 23 '19 at 12:12
  • it return false. try it by: IsItHere('ahhellllloou','hello') the output is NO but there is a 'hello' in correct sequence in first string. Rakesh code work properly. – Ali Tajik Apr 23 '19 at 12:45