2. Strings
🧸 ELI5: A string is just an array of letter-beads on a necklace string. "CAT" is 3 beads in a row: C, A, T.
🌍 Real life: A text message. A license plate. A word in your favorite book.
📝 Pseudocode:
word = "cat"
for each letter in word:
print(letter)
🐹 Go:
package main
import "fmt"
func main() {
word := "cat"
for i, letter := range word {
fmt.Printf("bead %d is %c\n", i, letter)
}
}
🇨 C (deep dive: real-life-c-guide.md §7):
#include <stdio.h>
int main(void) {
const char *word = "cat"; // in C, a "string" IS just an array of letter-beads + '\0'
for (int i = 0; word[i] != '\0'; i++) {
printf("bead %d is %c\n", i, word[i]);
}
return 0;
}
⏱️ Complexity: Reading a character by position = O(1). Scanning the whole word = O(n).