Program 18
Write a function called first_diff that is given two strings and returns the first location in which the strings differ. If the strings are identical, it should return -1.
def first_diff(s1,s2):
if(s1==s2):
return(-1)
else:
if len(s1)==len(s2):
for i in range(len(s2)):
if s1[i]!=s2[i]:
return(i+1)
s1=input("Enter the string1:")
s2=input("Enter the string2:")
c=first_diff(s1,s2)
if(c==-1):
print("Both Strings are Identical")
else:
print("first Difference occurs at location: ",c)
OUTPUT:
========= RESTART: D:\old\r19 lab-python\18.py ================
Enter the string1:crr
Enter the string2:crr
Both Strings are Identical
>>>
============ RESTART: D:\old\r19 lab-python\18.py =============
Enter the string1:crrcoe
Enter the string2:crrcse
first Difference occurs at location: 5
>>>
No comments:
Post a Comment