Home > Enterprise >  how do i make the program find a string to work properly?
how do i make the program find a string to work properly?

Time:08-03

i am looking to input two strings s1 and s2 and see if string s2 is in string s1 and if so where is it in string s1. But there is an error that causes the program to run nothing. can you help me find the error and help me fix it? The language my program is using is Python. Thank you!

import os
os.system('cls')

print('nhap chuoi 1: ',end='')
s1=str(input())
print('nhap chuoi 2: ',end='')
s2=str(input())
tontai=s2 in s1
if tontai==True:
    i=0
    while i<=len(s1)-1:
        v=0
        if s2[v]==s1[i]:
            j=i 1
            kt=False
            for j in range(i 1,i (len(s2))):
                v =1
                if s1[j]==s2[v]:kt=True
                else:
                    kt=False
                    break;
            if kt==True: 
                print(i,end=' ')
                i=i len(s2)
        else:i =1
else:print('chuoi 2 khong nam trong chuoi 1')
stop=input()

CodePudding user response:

Python has a function to do what you want:

import os
os.system('cls')

s1 = input('Enter string 1: ')
s2 = input('Enter string 2: ')
pos = s1.find(s2)
if pos == -1:
    print('s2 is not in s1')
else:
    print('s2 is in s1 at the position', pos)
  • Related