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.
Join strings together using the +
operator:
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
Get the number of characters in a string (conceptually):
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
Compare strings using comparison operators:
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
Algorithm EmptyCheck
var name = Input "Enter your name:"
If name == "" Then
Print "❌ Name cannot be empty!"
Else
Print "✅ Hello,", name
Endif
Endalgorithm
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
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
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
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
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
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
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
+
operator==
and !=
== ""