Tracks
/
Cairo
Cairo
/
Exercises
/
Welcome To Tech Palace!
Welcome To Tech Palace!

Welcome To Tech Palace!

Learning Exercise

Introduction

Cairo doesn't have a native type for strings but provides two ways to handle them: short strings using simple quotes and ByteArray using double quotes.

A short string is an ASCII string where each character is encoded on one byte.

The type Cairo uses for short strings by default is felt252, making their maximum length only 31 characters.

// below are different ways to write the same value
let name: felt252 = 'Jane';     // short string format
let name: felt252 = 0x4a616e65; // byte format
let name: felt252 = 1247899237; // decimal format

Technically, any integer type smaller than felt252 can be represented using the short string format.

// all of the below are equal u8 values, they are just written using different formats!
let lowercase_a_in_ascii: u8 = 'a';  // short string format
let lowercase_a_in_ascii: u8 = 0x61; // byte format
let lowercase_a_in_ascii: u8 = 97;   // decimal format

For strings longer than 31 characters Cairo provides ByteArray, which has an unlimited length.

let long_string: ByteArray = "this is a string which has more than 31 characters";

ByteArrays can be concatenated via the + operator:

"Jane" + " " + "Austen"
// => "Jane Austen"

Some special characters need to be escaped with a leading backslash, such as \t for a tab and \n for a new line in strings.

"How is the weather today?\nIt's sunny"  
// =>
// How is the weather today?
// It's sunny

You can even access individual bytes within a ByteArray using its index.

Be careful though, the type of the returned byte is u8 and not ByteArray!

let hello: ByteArray = "Hello World!";
let exclamation_mark: u8 = hello[11];
// => 33

The core library provides many useful methods and operators to work on ByteArrays.

For more information about ByteArray methods, check out the ByteArrayTrait documentation.

Here are some examples:

let name: felt252 = 'Jane';
let mut greeting: ByteArray = "Welcome ";

// append_word appends a single word of a given number of bytes to the end of the ByteArray 
greeting.append_word(name, 4);
// => "Welcome Jane"

// rev returns a ByteArray with the all characters reversed
let rev_greeting = greeting.rev();
// => "enaJ emocleW"

Instructions

There is an appliance store called "Tech Palace" nearby. The owner of the store recently installed a big display to use for marketing messages and to show a special greeting when customers scan their loyalty cards at the entrance. The display consists of lots of small LED lights and can show multiple lines of text.

The store owner needs your help with the code that is used to generate the text for the new display.

1. Create the welcome message

For most customers who scan their loyalty cards, the store owner wants to see Welcome to the Tech Palace, followed by the name of the customer in capital letters on the display.

Implement the function welcome_message that accepts the name of the customer as a short string (felt252) argument and returns the desired message as a ByteArray.

welcome_message("Judy")
// => Welcome to the Tech Palace, JUDY

2. Add a fancy border

For loyal customers that buy a lot at the store, the owner wants the welcome display to be more fancy by adding a line of stars before and after the welcome message. They are not sure yet how many stars should be in the lines so they want that to be configurable.

Write a function add_border that accepts a welcome message (a ByteArray) and the number of stars per line (type u32) as arguments. It should return a ByteArray that consists of 3 lines, a line with the desired number of stars, then the welcome message as it was passed in, then another line of stars.

add_border("Welcome!", 10)

Should return the following:

**********
Welcome!
**********

3. Clean up old marketing messages

Before installing this new display, the store had a similar display that could only show non-configurable, static lines. The owner would like to reuse some of the old marketing messages on the new display. However, the data already includes a star border and some unfortunate whitespaces. Your task is to clean up the messages so they can be re-used.

Implement a function clean_up_message that accepts the old marketing message as a ByteArray. The function should first remove all stars from the text and afterwards remove the leading and trailing whitespaces from the remaining text. The function should then return the cleaned up message.

let message: ByteArray = "
**************************
*    BUY NOW, SAVE 10%   *
**************************
";

clean_up_message(message)
// => BUY NOW, SAVE 10%
Edit via GitHub The link opens in a new window or tab
Cairo Exercism

Ready to start Welcome To Tech Palace!?

Sign up to Exercism to learn and master Cairo with 25 concepts68 exercises, and real human mentoring, all for free.