This lesson is a lot of fun.
We’ll integrate what we’ve learned recently about
Strings, algortihms, and then functions.
And then we’ll get some practice approaching a few new problems using
But first, we have a few new things to cover.
When we started working with variables and Kotlin’s eight basic types, we observed that there were certain types of assignments that would fail. For example:
Unlike some other languages, Kotlin will not automatically convert a value even if you might think it could be done safely:
So let’s look at how we can force Kotlin to perform certain type changes when necessary.
Strings Are Immutable
Strings Are Immutable
One of the important things to understand about Kotlin’s
Strings is that none of the methods that we can call on them change that
Instead, they always return a new
String that may be modified in some way.
Let’s see that in action:
Let's get some more practice working with
Strings: an incredibly useful data type for working with text.
Write a function called
reverse. It should accept a single
String argument and return that
There are several ways to approach this problem—have fun! And maybe take a good look at the documentation before you
write too much code...
We’ll be working with arrays of
Strings in this lesson.
Given that previously, when we used an array of
Ints we declared it as
IntArray and initialized it using
intArrayOf, and when we used an array of
Doubles we declared it as
DoubleArray and initialized it using
doubleArrayOf, you might assume that an array of
Strings would be
StringArray and intialized using
But not quite! Instead, the actual syntax is a bit different:
On the left we see the type
Array<String>, which indicates that this is an
array that contains
The type of the contents of the array is inside the angle brackets:
On the right we see the generic array initializer for Kotlin, which you can use
to create arrays of any type.
As usual, we don’t need to specify the type explicitly on the left:
How does Kotlin know what type of array it is?
Based on the arguments to
Since they are all
Strings, the type is inferred as
For now we’ll work with these arrays without worrying too much about what’s
going on under the hood.
Just be careful with
arrayOf, since if you mix the types of the arguments
things won’t work out as expected:
Now let’s have some fun and write a few new algorithms that work on
First, let’s try and write a function that determines if a
String contains a particular character.
We’ll sketch our our algorithm first, explore some potentially useful
String methods, and construct and test a solution.
Frequently when computers work with text data, we are processing data that was itself created by a machine. Or set up for machines to easily process.
One example is data stored in comma-separated-value format.
Let’s say we wanted to track how many people were tested for some random respiratory illness each day.
We might save that data in a
String that looked like this:
(Note that this is not real data!)
Let’s experiment with how we might work with this kind of data.
We’ll use some existing
String methods and a few new ones.
We’ll also use another unfamiliar method:
String.toInt, which allows us to convert a
String to an
Write a method name
flipHalves that returns a passed
String rearranged as follows.
You should move the second half of the string to the front, and the first half to the back.
So given the
String "CS" you would return "SC", and given the
String "testme" you would return "tmetes".
However, if the length of the
String is odd the middle character should remain in place.
So given the
String "CS124" you would return "241CS".
If the passed
String is empty, just return the empty
You will definitely want to review the
String method, and also spend some time experimenting with it
before you begin crafting your solution.
You also may want to consider odd and even length
Need more practice? Head over to the practice page.