-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_reverse_between_char.py
51 lines (45 loc) · 1.04 KB
/
ft_reverse_between_char.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def ft_len(str):
length = 0
for c in str:
length += 1
return length
def ft_reverse_str(str):
res = ""
for i in range(ft_len(str) - 1, -1, -1):
res += str[i]
return res
def ft_char_count(char, str):
count = 0
for i in range(ft_len(str)):
if str[i] == char:
count += 1
return count
def ft_reveres_between_char(char, str):
count = ft_char_count(char, str)
count1 = 0
count2 = 0
length = ft_len(str)
rev_str = ft_reverse_str(str)
i1 = 0
j1 = 0
i = 0
j = 0
while count2 < 1 and i < length - 1:
if str[i] == char:
i1 = i
count2 += 1
i += 1
while count1 < 1 and j < length - 1:
if rev_str[j] == char:
j1 = j
count1 += 1
j += 1
l_char = length - j1
rev_bet_char = ft_reverse_str(str[i1:l_char])
new_str = str[:i1] + rev_bet_char + str[l_char:]
if count >= 2:
return new_str
if count == 1:
return -1
if count == 0:
return -2