Hey I'm trying to create a function that takes in a string and a letter as arguments, identifies if there are two of this letter within the string, and then returns the number of characters between the two plus the letters. So 'Saturday' and 'a' would return 6.
def sublength(string, char):
new_string = ''
for i in string:
if i == char:
new_string.append(i)
if i != char:
new_string.append(i)
if i == char:
new_string.append(i)
break
return len(new_string)
What I want to do is iterate through string until char is found, add this character to new_string, continue to iterate and add the subsequent characters until char is found again, add it and then stop. This would be done in one iteration. However, as it's currently constructed, my function iterates through all of the string looking for matches and then stops. How do I either do this in one iteration or break up the string to achieve the same functionality? Thanks!
If you can provide a solution in JS as well I would really appreciate it!!