Lesson 13 • Intermediate

String Operations

25 minutes
Data Manipulation

What are Strings?

Strings are sequences of characters - essentially text. While we've used strings for display, this lesson covers how to manipulate, analyze, and transform text data.

Why String Operations Matter
String manipulation is crucial for:
  • Processing user input
  • Validating data (emails, passwords)
  • Text formatting and display
  • Searching and parsing text
  • Building messages and reports

➕ String Concatenation

Join strings together using the + operator:

Example: String Concatenation
Algorithm StringConcat

var firstName = "John"
var lastName = "Smith"

# Concatenate with space
var fullName = firstName + " " + lastName
Print "Full name:", fullName

# Build a sentence
var age = 25
var message = firstName + " is " + age + " years old"
Print message

# Email address builder
var username = "john.smith"
var domain = "example.com"
var email = username + "@" + domain
Print "Email:", email

Endalgorithm
Output
Full name: John Smith John is 25 years old Email: john.smith@example.com

📏 String Length

Get the number of characters in a string (conceptually):

Example: Counting Characters
Algorithm CountCharacters

var text = "Hello"

# Count manually using a loop
var count = 0
var i = 0

# In real implementation, you'd use a Length() function
# For now, we demonstrate the concept

Print "Text:", text
Print "(String has 5 characters)"

Endalgorithm

String Comparison

Compare strings using comparison operators:

Example: String Comparison
Algorithm StringComparison

var password = "secret123"
var input = Input "Enter password:"

If input == password Then
    Print "✅ Access granted!"
Else
    Print "❌ Access denied!"
Endif

# Check if strings are different
var name1 = "Alice"
var name2 = "Bob"

If name1 != name2 Then
    Print "Different names"
Endif

Endalgorithm

🔤 Common String Patterns

1. Check if String is Empty

Example: Empty String Check
Algorithm EmptyCheck

var name = Input "Enter your name:"

If name == "" Then
    Print "❌ Name cannot be empty!"
Else
    Print "✅ Hello,", name
Endif

Endalgorithm

2. Count Specific Characters

Example: Count Vowels
Algorithm CountVowels

var text = Input "Enter a sentence:"
var vowelCount = 0

# Note: In a real implementation, you'd iterate through each character
# This demonstrates the logic conceptually

Print "Text:", text
Print "(In real implementation, would count a, e, i, o, u)"

Endalgorithm

3. String Building with Loop

Example: Repeat String
Algorithm RepeatString

var word = Input "Enter a word:"
var times = Input "Repeat how many times?"

var result = ""

For i = 1 To times
    result = result + word + " "
Endfor

Print "Result:", result

Endalgorithm

Real-World Applications

Email Validator

Example: Simple Email Validation
Algorithm EmailValidator

var email = Input "Enter your email:"

var hasAt = false
var hasDot = false
var isEmpty = email == ""

# Note: In real implementation, you'd check for @ and . characters
# This shows the validation logic conceptually

If isEmpty Then
    Print "❌ Email cannot be empty"
Else
    Print "Email entered:", email
    Print "(Would validate @ and . presence)"
Endif

Endalgorithm

Username Generator

Example: Username Generator
Algorithm UsernameGenerator

var firstName = Input "Enter first name:"
var lastName = Input "Enter last name:"
var birthYear = Input "Enter birth year:"

# Generate username: first letter of first name + last name + year
# Example: John Smith 1990 -> jsmith1990

Print ""
Print "=== GENERATED USERNAMES ==="

# Option 1: firstname.lastname
var username1 = firstName + "." + lastName
Print "Option 1:", username1

# Option 2: firstname + year
var username2 = firstName + birthYear
Print "Option 2:", username2

# Option 3: lastname + year
var username3 = lastName + birthYear
Print "Option 3:", username3

Endalgorithm

Text Formatter

Example: Text Formatter
Algorithm TextFormatter

var title = Input "Enter title:"
var author = Input "Enter author:"
var date = Input "Enter date:"

# Create formatted header
var separator = "================================"

Print ""
Print separator
Print "       DOCUMENT HEADER"
Print separator
Print ""
Print "Title:  ", title
Print "Author: ", author
Print "Date:   ", date
Print ""
Print separator

Endalgorithm

Message Template System

Example: Message Templates
Algorithm MessageTemplates

var userName = Input "Enter user name:"
var messageType = Input "Message type (1=Welcome, 2=Farewell, 3=Error):"

var message = ""

If messageType == 1 Then
    message = "Welcome, " + userName + "! We're glad to have you here."
Else If messageType == 2 Then
    message = "Goodbye, " + userName + ". See you soon!"
Else If messageType == 3 Then
    message = "Error: " + userName + ", something went wrong. Please try again."
Else
    message = "Invalid message type."
Endif

Print ""
Print message

Endalgorithm

🔐 Password Strength Checker

Example: Password Strength
Algorithm PasswordStrength

var password = Input "Enter password:"

var strength = 0
var feedback = ""

# Check length (simulated)
Print ""
Print "=== PASSWORD STRENGTH CHECK ==="

If password == "" Then
    feedback = "❌ Password cannot be empty"
Else
    feedback = "✅ Password entered"
    Print feedback
    Print "(Would check: length, uppercase, lowercase, numbers, special chars)"
Endif

Endalgorithm

String Manipulation Tips

Best Practices
  • Validate Input: Always check for empty strings
  • Build Gradually: Use concatenation to build complex strings
  • Be Descriptive: Use meaningful variable names for strings
  • Format Consistently: Keep formatting patterns consistent
  • Handle Edge Cases: Check for special characters, spaces, etc.

Key Takeaways

  • ✅ Strings are sequences of characters (text)
  • ✅ Concatenate strings using + operator
  • ✅ Compare strings with == and !=
  • ✅ Check for empty strings with == ""
  • ✅ Build complex strings by concatenating simpler parts
  • ✅ Always validate string input from users
  • ✅ Use strings for text processing, formatting, and validation

💪 Practice Exercises

Try These Exercises

Exercise 1: Full Name Formatter
Create a program that gets first, middle, and last names, then displays them in different formats (Last, First Middle; First M. Last; etc.).
Exercise 2: Word Counter
Build a program that counts how many times a specific word appears in a sentence (using loops and comparison).
Exercise 3: URL Builder
Create a program that builds URLs from components: protocol (http/https), domain, path, and parameters.
Exercise 4: Initials Generator
Write a program that takes a full name and generates initials (e.g., "John Paul Smith" → "JPS").
Excellent Work!
You now understand how to manipulate and work with text using strings! In the next lesson, we'll explore multidimensional arrays for working with 2D data like grids and matrices.