Exercise 83 *
Write a Python program as function that takes as parameter a string s and which returns the first repeated character in the string s. Example: if s = "django framework", the fuction returns the character 'a'
Solution
# create a function to test if given character is repeated within a given string
def isRepeated(s,c):
counter = 0
for x in s:
if x == c:
counter = counter + 1
if counter >= 2:
return True
else:
return False
# function which determines the first repeated character
def firstRepeated(s):
repeated = ''
for x in s:
if isRepeated(s,x):
repeated = x
break
return repeated
# Example
s = "django framework"
print("The first repeated character is : " , firstRepeated(s))
# The output is: 'The first repeated character is : a'
Younes Derfoufi
my-courses.net
my-courses.net