Arrays and Strings
Twinkies provides built-in support for arrays and strings with automatic bounds checking and convenient operations.
Arrays
Arrays in Twinkies are fixed-size collections of elements of the same type.
Array Declaration
Arrays are declared with a type and size:
let numbers: int[5]; // Array of 5 integers
let flags: bool[10]; // Array of 10 booleans
let names: string[3]; // Array of 3 strings
let matrix: float[3][3]; // Multi-dimensional array
Array Initialization
Arrays can be declared without initialization:
let arr: int[5]; // Uninitialized array
Arrays are zero-initialized by default (implementation-dependent).
Array Access
Arrays are accessed using square brackets with zero-based indexing:
let arr: int[5];
arr[0] = 10; // First element
arr[1] = 20; // Second element
arr[4] = 50; // Last element (indices 0-4)
Array Bounds Checking
Twinkies automatically checks array bounds:
Compile-time checking for constant indices:
let arr: int[5];
arr[4] = 10; // ✓ Valid (0-4 are valid indices)
arr[10] = 20; // ✗ Compile-time error: index 10 out of bounds
Runtime checking for dynamic indices:
let arr: int[5];
let i: int = input();
arr[i] = 10; // Runtime check generated
Iterating Over Arrays
Use loops to iterate over arrays:
let arr: int[5];
let i: int = 0;
// Initialize array
while (i < 5) {
arr[i] = i * 2;
i = i + 1;
}
// Print array
i = 0;
while (i < 5) {
print(arr[i]);
i = i + 1;
}
Array Examples
Finding Maximum
func find_max(arr: int[10]) -> int {
let max: int = arr[0];
let i: int = 1;
while (i < 10) {
if (arr[i] > max) {
max = arr[i];
}
i = i + 1;
}
return max;
}
Sum of Array
func sum_array(arr: int[5]) -> int {
let total: int = 0;
let i: int = 0;
while (i < 5) {
total = total + arr[i];
i = i + 1;
}
return total;
}
Array Reversal
func reverse_array(arr: int[5]) -> void {
let i: int = 0;
let j: int = 4;
while (i < j) {
let temp: int = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i = i + 1;
j = j - 1;
}
}
Strings
Strings in Twinkies are sequences of characters with built-in operations.
String Declaration
let greeting: string = "Hello, World!";
let empty: string = "";
let name: string = "Twinkies";
String Literals
String literals are enclosed in double quotes:
let message: string = "This is a string";
let multiline: string = "Line 1\nLine 2"; // With escape sequences
Escape Sequences
Strings support escape sequences:
\n- newline\t- tab\\- backslash\"- double quote
let text: string = "First line\nSecond line";
let path: string = "C:\\Users\\Name";
let quote: string = "He said \"Hello\"";
String Indexing
Strings can be indexed to get individual characters:
let str: string = "Hello";
let first: string = str[0]; // "H"
let second: string = str[1]; // "e"
let last: string = str[4]; // "o"
String indexing returns a single-character string, not a character type.
String Operations
String Concatenation
Use the + operator or concat() function:
let a: string = "Hello";
let b: string = "World";
let combined: string = a + " " + b; // "Hello World"
let result: string = concat(a, b); // "HelloWorld"
String Length
let str: string = "Hello";
let len: int = strlen(str); // 5
String Substring
let str: string = "Hello, World!";
let sub: string = substr(str, 0, 5); // "Hello"
// substr(string, start_index, length)
String Comparison
let str1: string = "Hello";
let str2: string = "World";
let cmp: int = strcmp(str1, str2);
// Returns: negative if str1 < str2, 0 if equal, positive if str1 > str2
String Functions Reference
| Function | Description | Example |
|---|---|---|
strlen(s) | Get string length | strlen("Hello") → 5 |
strcmp(s1, s2) | Compare strings | strcmp("a", "b") → negative |
substr(s, start, len) | Extract substring | substr("Hello", 0, 2) → "He" |
concat(s1, s2) | Concatenate strings | concat("Hello", "World") → "HelloWorld" |
String Examples
String Reversal
func reverse_string(s: string) -> string {
let len: int = strlen(s);
let result: string = "";
let i: int = len - 1;
while (i >= 0) {
result = concat(result, s[i]);
i = i - 1;
}
return result;
}
Check if String Contains Character
func contains_char(s: string, c: string) -> bool {
let len: int = strlen(s);
let i: int = 0;
while (i < len) {
if (s[i] == c) {
return true;
}
i = i + 1;
}
return false;
}
String to Number (Simple)
func string_to_int(s: string) -> int {
let result: int = 0;
let len: int = strlen(s);
let i: int = 0;
while (i < len) {
let digit: string = s[i];
// Simple conversion (assumes single digit)
if (digit == "0") {
result = result * 10 + 0;
} else if (digit == "1") {
result = result * 10 + 1;
}
// ... (simplified example)
i = i + 1;
}
return result;
}
Array of Strings
Arrays can hold strings:
let names: string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
let i: int = 0;
while (i < 3) {
print(names[i]);
i = i + 1;
}
Multi-dimensional Arrays
Twinkies supports multi-dimensional arrays:
let matrix: int[3][3]; // 3x3 matrix
// Initialize matrix
let i: int = 0;
while (i < 3) {
let j: int = 0;
while (j < 3) {
matrix[i][j] = i * 3 + j;
j = j + 1;
}
i = i + 1;
}
Array and String Limitations
- Fixed Size - Arrays must have a compile-time constant size
- No Dynamic Arrays - Cannot resize arrays at runtime
- No Array Slicing - Cannot create sub-arrays easily
- String Immutability - Strings are immutable (operations return new strings)
- No String Formatting - No printf-style formatting (use concatenation)
Best Practices
- Always check bounds - The compiler does this automatically, but be aware
- Use meaningful array sizes - Choose appropriate sizes for your use case
- Initialize arrays - Explicitly initialize arrays when needed
- Use loops for iteration - Always use loops to process arrays
- Handle empty strings - Check for empty strings before operations
- Use string functions - Prefer built-in string functions over manual operations
Common Patterns
Array Initialization Pattern
let arr: int[10];
let i: int = 0;
while (i < 10) {
arr[i] = i * 2; // Initialize with pattern
i = i + 1;
}
Array Search Pattern
func find_in_array(arr: int[10], target: int) -> int {
let i: int = 0;
while (i < 10) {
if (arr[i] == target) {
return i; // Found at index i
}
i = i + 1;
}
return -1; // Not found
}
String Building Pattern
func build_message(name: string, age: int) -> string {
let message: string = "Hello, ";
message = concat(message, name);
message = concat(message, "! You are ");
// ... (would need number-to-string conversion)
return message;
}
Next Steps
- Learn about Functions for functions that work with arrays
- Read Control Flow for iterating over arrays
- Check Examples Guide for array and string examples
