Given a string, print its even-indexed and odd-indexed characters as space-separated strings on a single line.
Example:
s = adbecf
=> Printabc def
My approach:
JavaScript
x
18
18
1
t = input()
2
p = len(t)
3
4
for i in range(p):
5
s = t[i]
6
n = len(s)
7
even = []
8
odd = []
9
for j in range(n):
10
if j % 2 == 0:
11
even.append(s[j])
12
for j in range(n):
13
if j % 2 != 0:
14
odd.append(s[j])
15
first_join=''.join(even)
16
second_join = ''.join(odd)
17
print("{} {}".format(first_join,second_join)
18
Advertisement
Answer
No need to use complex ways. here is a easy way to do it.
JavaScript
1
10
10
1
t = input()
2
p = len(t)
3
ans = "" # create empty string
4
for i in range(0,p,2):
5
ans += t[i] # Add even characters
6
ans += " " # Add space.
7
for j in range(1,p,2):
8
ans += t[j] # Add odd characters
9
print(ans)
10
Input: adbecf
Output: abc def