Arrays, Strings And Data Handling: String Handling Operations In Pseudocode (Copy)
String Handling Operations In Pseudocode
What String Handling Means
- String handling involves:
- Storing text
- Accessing characters
- Comparing text
- Modifying text
- Validating input
- Strings are treated as:
- A sequence of characters
- Indexed character-by-character
- Common uses:
- Names
- IDs
- Passwords
- User input validation
- Text processing
Core Properties Of Strings (Exam-Critical)
| Property | Explanation |
|---|---|
| Data type | STRING |
| Length | Number of characters |
| Indexing | Characters accessed by position |
| Mutability | Modified via reassignment |
| Comparison | Lexicographic / exact match |
Declaring And Assigning Strings
Declaration
DECLARE name : STRING
DECLARE password : STRING
Assignment
name ← "Ali"
password ← "abc123"
- Strings are enclosed in quotation marks
- Spaces count as characters
String Indexing Rules
Accessing Individual Characters
char ← name[1]
- Accesses first character
- Indexing starts from 1
Example
name ← "Sara"
OUTPUT name[2]
- Output:
a
Finding Length Of A String
LENGTH Function
len ← LENGTH(name)
- Returns number of characters
- Includes spaces
Example
name ← "Ali Khan"
len ← LENGTH(name)
len = 8
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Traversing A String Using A Loop
Standard String Traversal Pattern
FOR i ← 1 TO LENGTH(text)
OUTPUT text[i]
NEXT i
- Treats string like a one-dimensional array of characters
Example: Count Vowels In A String
count ← 0
FOR i ← 1 TO LENGTH(word)
IF word[i] = "A" OR word[i] = "E" OR word[i] = "I" OR
word[i] = "O" OR word[i] = "U" THEN
count ← count + 1
ENDIF
NEXT i
Comparing Strings
Exact Comparison
IF password = "admin" THEN
OUTPUT "Access Granted"
ENDIF
- Case-sensitive comparison
"Admin"≠"admin"
Comparing User Input
INPUT guess
IF guess = secret THEN
OUTPUT "Correct"
ELSE
OUTPUT "Incorrect"
ENDIF
Partial String Comparison (Character-Based)
Check First Character
IF id[1] = "A" THEN
OUTPUT "Valid ID"
ENDIF
Check Last Character
IF text[LENGTH(text)] = "!" THEN
OUTPUT "Exclamation detected"
ENDIF
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Concatenation (Joining Strings)
Basic Concatenation
fullName ← firstName & " " & lastName
Example
first ← "Ali"
last ← "Khan"
full ← first & " " & last
- Result:
"Ali Khan"
Modifying Strings (Reassignment-Based)
Replace Entire String
name ← "Ahmed"
Build A New String Character By Character
newText ← ""
FOR i ← 1 TO LENGTH(oldText)
newText ← newText & oldText[i]
NEXT i
Remove Spaces From String
result ← ""
FOR i ← 1 TO LENGTH(text)
IF text[i] <> " " THEN
result ← result & text[i]
ENDIF
NEXT i
Extracting Part Of A String (Substring Logic)
Extract First N Characters
sub ← ""
FOR i ← 1 TO n
sub ← sub & text[i]
NEXT i
Extract Characters Between Two Positions
sub ← ""
FOR i ← start TO end
sub ← sub & text[i]
NEXT i
String Validation Patterns (Very Common)
Validate Minimum Length
IF LENGTH(password) >= 8 THEN
OUTPUT "Valid"
ELSE
OUTPUT "Too short"
ENDIF
Validate Starts With Specific Character
IF code[1] = "X" THEN
OUTPUT "Accepted"
ENDIF
Validate Contains A Digit
hasDigit ← FALSE
FOR i ← 1 TO LENGTH(text)
IF text[i] >= "0" AND text[i] <= "9" THEN
hasDigit ← TRUE
ENDIF
NEXT i
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common String Processing Tasks
Count Characters
count ← LENGTH(text)
Count Specific Character
count ← 0
FOR i ← 1 TO LENGTH(text)
IF text[i] = "A" THEN
count ← count + 1
ENDIF
NEXT i
Reverse A String
reverse ← ""
FOR i ← LENGTH(text) TO 1 STEP -1
reverse ← reverse & text[i]
NEXT i
Convert To Uppercase (Logic-Based)
FOR i ← 1 TO LENGTH(text)
IF text[i] >= "a" AND text[i] <= "z" THEN
newText ← newText & <uppercase version>
ELSE
newText ← newText & text[i]
ENDIF
NEXT i
- Conceptual logic only
- Actual ASCII conversion not required unless specified
Strings With Arrays (Hybrid Use)
Store Multiple Strings In Array
DECLARE names : ARRAY[1..10] OF STRING
Process Each String
FOR i ← 1 TO 10
OUTPUT LENGTH(names[i])
NEXT i
Common Logic Errors In String Handling
Error 1: Index Out Of Range
text[LENGTH(text) + 1]
Error 2: Forgetting Strings Are Case-Sensitive
IF answer = "yes"
"Yes"will fail
Error 3: Modifying Without Reassignment
text[i] ← "A"
- Invalid
- Must rebuild string
Error 4: Comparing Characters To Numbers Incorrectly
IF text[i] > 5
- Invalid comparison
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Dry-Run Technique For String Questions
- Write string with index numbers
- Track loop index
- Track string being built
- Confirm reassignment happens correctly
Exam-Ready String Operation Templates
Template 1: Traverse String
FOR i ← 1 TO LENGTH(text)
<process text[i]>
NEXT i
Template 2: Build New String
result ← ""
FOR i ← 1 TO LENGTH(text)
IF <condition> THEN
result ← result & text[i]
ENDIF
NEXT i
Template 3: Validate String
IF LENGTH(text) >= n AND text[1] = "A" THEN
OUTPUT "Valid"
ENDIF
Examiner Checklist For Full Marks
- Correct string declaration
- Correct indexing
- Correct LENGTH usage
- Reassignment used for modification
- Case sensitivity handled
- No out-of-range access
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Strings are character arrays
- Indexing starts at 1
- LENGTH controls traversal
- Modification requires rebuilding
- Case sensitivity always matters
