Arrays, Strings And Data Handling: Character-By-Character String Processing (Copy)
Character-By-Character String Processing
What Character-By-Character Processing Means
- Processing a string one character at a time
- Treating a string like a one-dimensional array of characters
- Accessing characters using index positions
- Essential for:
- Validation
- Pattern checking
- Counting specific characters
- Filtering characters
- Building new strings
Core Examiner Rule (Non-Negotiable)
- You cannot directly modify a character inside a string
- Any change requires:
- Reading characters one by one
- Building a new string
- Reassigning it
String As A Sequence Of Characters
Conceptual Model
text = "HELLO"
Index: 1 2 3 4 5
Chars: H E L L O
text[1]→"H"text[5]→"O"
Standard Character Traversal Pattern (MOST IMPORTANT)
FOR i ← 1 TO LENGTH(text)
<process text[i]>
NEXT i
- This pattern appears in almost every string-processing question
- Examiner expects:
- Correct bounds
- Correct index usage
Reading Characters One By One
Example 1: Output Each Character On New Line
FOR i ← 1 TO LENGTH(word)
OUTPUT word[i]
NEXT i
Example 2: Output Characters With Position
FOR i ← 1 TO LENGTH(word)
OUTPUT i, ":", word[i]
NEXT i
Counting Characters Using Conditions
Example 3: Count Vowels
count ← 0
FOR i ← 1 TO LENGTH(text)
IF text[i] = "A" OR text[i] = "E" OR text[i] = "I" OR
text[i] = "O" OR text[i] = "U" THEN
count ← count + 1
ENDIF
NEXT i
Example 4: Count Digits
count ← 0
FOR i ← 1 TO LENGTH(text)
IF text[i] >= "0" AND text[i] <= "9" THEN
count ← count + 1
ENDIF
NEXT i
Example 5: Count Spaces
count ← 0
FOR i ← 1 TO LENGTH(text)
IF text[i] = " " THEN
count ← count + 1
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
Character-Based Validation (VERY COMMON)
Example 6: Validate First Character
IF code[1] = "A" THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
Example 7: Validate Last Character
IF text[LENGTH(text)] = "." THEN
OUTPUT "Sentence complete"
ENDIF
Example 8: Check If String Contains At Least One Digit
hasDigit ← FALSE
FOR i ← 1 TO LENGTH(text)
IF text[i] >= "0" AND text[i] <= "9" THEN
hasDigit ← TRUE
ENDIF
NEXT i
Example 9: Check If String Contains Only Letters
valid ← TRUE
FOR i ← 1 TO LENGTH(text)
IF NOT (text[i] >= "A" AND text[i] <= "Z") AND
NOT (text[i] >= "a" AND text[i] <= "z") THEN
valid ← FALSE
ENDIF
NEXT i
Building New Strings Character By Character
Core Rule
- Strings are rebuilt, not edited
- Use concatenation (
&)
Example 10: Copy A String
newText ← ""
FOR i ← 1 TO LENGTH(text)
newText ← newText & text[i]
NEXT i
Example 11: Remove Spaces
result ← ""
FOR i ← 1 TO LENGTH(text)
IF text[i] <> " " THEN
result ← result & text[i]
ENDIF
NEXT i
Example 12: Remove Digits
result ← ""
FOR i ← 1 TO LENGTH(text)
IF NOT (text[i] >= "0" AND text[i] <= "9") THEN
result ← result & text[i]
ENDIF
NEXT i
Example 13: Extract Only Uppercase Letters
result ← ""
FOR i ← 1 TO LENGTH(text)
IF text[i] >= "A" AND text[i] <= "Z" THEN
result ← result & text[i]
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
Transforming Characters (Logic-Based)
Example 14: Replace Spaces With Underscores
result ← ""
FOR i ← 1 TO LENGTH(text)
IF text[i] = " " THEN
result ← result & "_"
ELSE
result ← result & text[i]
ENDIF
NEXT i
Example 15: Mask Characters (Password Style)
masked ← ""
FOR i ← 1 TO LENGTH(password)
masked ← masked & "*"
NEXT i
Example 16: Reverse A String
reverse ← ""
FOR i ← LENGTH(text) TO 1 STEP -1
reverse ← reverse & text[i]
NEXT i
Position-Specific Character Logic
Example 17: Check Alternate Characters
FOR i ← 1 TO LENGTH(text) STEP 2
OUTPUT text[i]
NEXT i
Example 18: Ignore First And Last Characters
FOR i ← 2 TO LENGTH(text) - 1
OUTPUT text[i]
NEXT i
Character Processing With Flags
Example 19: Detect Consecutive Spaces
previousSpace ← FALSE
FOR i ← 1 TO LENGTH(text)
IF text[i] = " " AND previousSpace = TRUE THEN
OUTPUT "Double space detected"
ENDIF
IF text[i] = " " THEN
previousSpace ← TRUE
ELSE
previousSpace ← FALSE
ENDIF
NEXT i
Character Processing Combined With Arrays
Example 20: Store Characters In Array
DECLARE chars : ARRAY[1..LENGTH(text)] OF STRING
FOR i ← 1 TO LENGTH(text)
chars[i] ← text[i]
NEXT i
Common Logic Errors In Character Processing
Error 1: Out-Of-Range Index
text[LENGTH(text) + 1]
Error 2: Trying To Modify Directly
text[i] ← "A"
- Invalid
- Must rebuild string
Error 3: Forgetting LENGTH In Loop Bound
FOR i ← 1 TO n
nnot defined- Must use
LENGTH(text)
Error 4: Case Sensitivity Ignored
IF text[i] = "a"
"A"will not match
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 Character Processing
- Write the string with index numbers
- Track loop index
- Track output string as it grows
- Apply condition for each character
- Confirm concatenation order
Exam-Ready Templates (Copy-Paste Safe)
Template 1: Character Traversal
FOR i ← 1 TO LENGTH(text)
<process text[i]>
NEXT i
Template 2: Filter Characters
result ← ""
FOR i ← 1 TO LENGTH(text)
IF <condition> THEN
result ← result & text[i]
ENDIF
NEXT i
Template 3: Transform Characters
result ← ""
FOR i ← 1 TO LENGTH(text)
IF <condition> THEN
result ← result & <new character>
ELSE
result ← result & text[i]
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
Examiner Checklist For Full Marks
- Correct use of LENGTH
- Correct index bounds
- No direct character assignment
- Correct concatenation
- Case sensitivity handled
- No out-of-range access
One-Line Rules To Memorise
- Strings are processed character by character
- Indexing starts at 1
- LENGTH controls loops
- Modification means rebuilding
- Every character must be checked deliberately
