Q:

Kotlin program of character range using downTo() function

belongs to collection: Kotlin Ranges Programs

0

The downTo() function is used for creating a range in descending order i.e., from the given larger value to the smaller value. The downTo() function is the reverse of the (..) operator and rangeTo() function.

Here, we will demonstrate the example of creating a character range using the downTo() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example 1:

fun main(args : Array<String>){
	// creating character ranges
	// in descending order
	println("Character range 1:")	
	for(x in 'g'.downTo('a')){
		println(x)
	}
	println()

	println("Character range 2:")	
	for(x in 'p'.downTo('g')){
		println(x)
	}
	println()
}

Output:

Character range 1:
g
f
e
d
c
b
a

Character range 2:
p
o
n
m
l
k
j
i
h
g

Example 2:

fun main(args : Array<String>){
	// creating character ranges
	// in descending order
	println("Character range 1:")	
	for(x in 'g'.downTo('a') step 3){
		println(x)
	}
	println()

	println("Character range 2:")	
	for(x in 'p'.downTo('g') step 3){
		println(x)
	}
	println()
}

Output:

Character range 1:
g
d
a

Character range 2:
p
m
j
g

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program of print the first, last, and step ... >>
<< Kotlin program of integer range using downTo() fun...