Lesson 8 - Strings in Kotlin - Working with single characters

Kotlin Basic constructs Strings in Kotlin - Working with single characters

In the previous lesson, Arrays in Kotlin, we learned to work with arrays. If you noticed some similarities between arrays and strings, you were absolutely onto something. For the others, it may be a surprise that a String is essentially an array of characters (Chars) and we can work with it like so.

First, we'll check out how it works by simply printing the character at a given position:

val s = "Hello ICT.social"
println(s)
println(s[2])

The output:

Hello ICT.social
l

We can see that we can access characters of a string through the brackets as it was with the array. It may be disappointing that characters at the given positions are read-only in Kotlin, so we can't write the following:

// This code won't work
var s = "Hello, ICT.social"
s[2] = "l"
println(s)

Of course, there is a way to do it, but we'll go over it later. For now, we'll be reading characters.

Character occurrence in a sentence analysis

Let's write a simple program that analyzes a given sentence for us. We'll search for the number of vowels, consonants and non-alphanumeric characters (e.g. space or !).

We'll hard-code the input string in our code, so we won't have to write it again every time. Once the program is complete, we'll replace the string with readLine(). We'll iterate over characters using a loop. I should start out by saying that we won't focus as much on program speed here, we'll choose practical and simple solutions.

First, let's define vowels and consonants. We don't have to count non-alphanumeric characters since it'll be the string length minus the number of vowels and consonants. Since we don't want to deal with the case, uppercase/lower­case, of the letters, we'll convert the entire string to lowercase at the start. Let's set up variables for the individual counters, also, because it is a more complex code, we'll add comments.

// the string that we want to analyze
var s = "A programmer gets stuck in the shower because the instructions on the shampoo were: Lather, Wash, and Repeat."
println(s)
s = s.toLowerCase()

// counters initialization
var vowelsCount = 0
var consonantsCount = 0

// definition of character groups
val vowels = "aeiouy"
val consonants = "bcdfghjklmnpqrstvwxz"

// main loop
for (c in s) {

}

First of all, we prepare the string and convert it to lowercase. Then, we reset the counters. For the definition of characters groups, we only need ordinary Strings. The main loop iterates over each character in the String s. In each iteration of the loop the variable c will contain the current character.

Now let's increment the counters. For simplicity's sake, I'll focus on the loop instead of rewriting the code repeatedly:

for (c in s) {
        if (vowels.contains(c)) {
                vowelsCount++
        } else if (consonants.contains(c)) {
                consonantsCount++
        }
}

We already know the contains() method on a string. As a parameter, it can take both a substring or a character. Firstly, we try to find the character c from our sentence in the string vowels and possibly increase their counter. If it's not included in vowels, we look in consonants and possibly increase their counter.

Now the last thing left is to print it at the end:

var s = "A programmer got stuck in shower because the label said: apply, wash, repeat."
println(s)
s = s.toLowerCase()

// counters initialization
var vowelCount = 0
var consonantCount = 0

// definition of character groups
val vowels = "aeiouy"
val consonants = "bcdfghjklmnpqrstvwxyz"

// main loop
for (c in s) {
        if (vowels.contains(c)) {
                vowelCount++
        } else if (consonants.contains(c)) {
                consonantCount++
        }
}

println("Vowels: $vowelCount")
println("Consonants: $consonantCount")
println("Other characters: ${s.length - (vowelCount + consonantCount)}")

The output:

A programmer gets stuck in the shower because the instructions on the shampoo were: Lather, Wash, and Repeat.
Vowels: 33
Consonants: 55
Non-alphanumeric characters: 21

That's it, we're done!

ASCII value

Maybe you've already heard about the ASCII table. Especially, in the MS-DOS era when there was practically no other way to store text. Individual characters were stored as numbers of the byte datatype, so of a range from 0 to 255. The system provided the ASCII table which had 256 characters and each ASCII code (numerical code) was assigned to one character.

Perhaps you understand why this method is no longer as relevant. The table simply could not contain all the characters of all international alphabets, now we use Unicode (UTF-8) encoding where characters are represented in a different way. In Kotlin, we have the option to work with ASCII values of individual characters. The main advantage is that the characters are stored in the table next to each other, alphabetically. For example, at the position 97 we can find "a", at 98 "b" etc. It's the same with numbers, but unfortunately, the accent characters are messed up.

Now, let's convert a character into its ASCII value and vice versa create the character according to its ASCII value.

var c: Char // character
var i: Int // ordinal (ASCII) value of the character
// conversion from text to ASCII value
c = 'a'
i = c.toInt()
println("The character $c was converted to its ASCII value of $i")

// conversion from an ASCII value to text
i = 98
c = i.toChar()
println("The ASCII value of $i was converted to its textual value of $c")

The output:

The character a was converted to its ASCII value of 97
The ASCII value of 98 was converted to its textual value of b

These conversions are called type casts, which we'll get into later on.

The Caesar cipher

Let's create a simple program to encrypt text. If you've ever heard of the Caesar cipher, then you already know exactly what we're going to program. The text encryption is based on shifting characters in the alphabet by a certain fixed number of characters. For example, if we shift the word "hello" by 1 character forwards, we'd get "ifmmp". The user will be allowed to select the number of character shifts.

Let's get right into it! We need variables for the original text, the encrypted message, and the shift. Then, we need a loop iterating over each character and printing an encrypted message. We'll also hard-code the message defined in the code, so we won't have to write it over and over during the testing phase. After we finish the program, we'll replace the contents of the variable with the readLine() method. The cipher doesn't work with accent characters, spaces and punctuation marks. We'll just assume the user will not enter them. Ideally, we should remove accent characters before encryption, as well as anything except letters.

// variable initialization
val s = "blackholesarewheregoddividedbyzero"
println("Original message: $s")
var message = ""
var shift = 1

// loop iterating over characters
for (c in s) {

}

// printing
println("Encrypted message: $message")

We'll now move into the loop. We'll cast the character in c to its ASCII value, its ordinal value, increase the value by however many shifts and cast it back to the character. This character will be added to the final message:

var i = c.toInt()
i += shift
val c = i.toChar()
message += c

Let's try it out! The result looks pretty good. However, we can see that the characters after "z" overflow to ASCII values of other characters (e.g. "{"). Therefore, the characters are no longer just alphanumeric, but other nasty characters. Let's enclose our characters as a cyclical pattern, so the shifting could flow smoothly from "z" to "a" and so on. We'll get by with a simple condition that decreases the ASCII value by the length of the alphabet so we'd end back up at "a".

var i = c.toInt()
i += shift
if (i > 'z'.toInt()) {
        i -= 26
}
val char = i.toChar()
message += char

If i exceeds the ASCII value of 'z', we reduce it by 26 characters (the number of characters in the English alphabet). The -= operator does the same as we would do with i = i - 26. It's simple and our program is now operational. Notice that we don't use direct character codes anywhere. There's a 'z'.toInt() in the condition even though we could write 122 there directly. We did it this way so that our program is fully encapsulated from explicit ASCII values, so it'd be clearer how it works. Try to code the decryption program as practice for yourself.

In the next lesson, Strings in Kotlin - Split, we'll see that there are still a couple more things we haven't touched base on that strings can do. Spoiler: We'll learn how to decode "Morse code".


 

Download

Downloaded 0x (16.01 kB)
Application includes source codes in language Kotlin

 

 

Article has been written for you by Samuel Kodytek
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
Thumbnail
Previous article
Arrays in Kotlin
Thumbnail
All articles in this section
Kotlin basic constructs
Activities (4)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!