CS11: Input, Output & String Handling
Mastering INPUT and OUTPUT commands, string operations (LENGTH, POSITION, SUBSTRING), concatenation, character codes, and type conversion.
Mastering INPUT and OUTPUT commands, string operations (LENGTH, POSITION, SUBSTRING), concatenation, character codes, and type conversion.
INPUT name // Get a string
INPUT "Enter your age: ", age // With prompt
age ← INT(INPUT("Enter age: ")) // Get and convert to integer
OUTPUT "Hello, World!" // Simple string OUTPUT score // Variable value OUTPUT "Your score is " & STR(score) // Combined string and variable OUTPUT "Total: ", total, " Average: ", avg // Multiple items
name ← "Alice" score ← 95 OUTPUT "Name: " & name & " | Score: " & STR(score) // Output: Name: Alice | Score: 95
word ← "Computer"
OUTPUT LENGTH(word) // 8
OUTPUT LENGTH("Hi") // 2
OUTPUT LENGTH("") // 0 (empty string)
text ← "Hello World" OUTPUT POSITION(text, "World") // 6 (found at position 6) OUTPUT POSITION(text, "xyz") // -1 (not found) OUTPUT POSITION(text, "o") // 5 (first occurrence)
text ← "Computer Science" OUTPUT SUBSTRING(text, 0, 3) // "Com" (from position 0, 3 characters) OUTPUT SUBSTRING(text, 9, 7) // "Science" (from position 9, 7 characters) OUTPUT SUBSTRING(text, 3, 3) // "put"
firstName ← "Alice" lastName ← "Smith" fullName ← firstName & " " & lastName OUTPUT fullName // "Alice Smith"
| Function | Purpose | Example | Result |
|---|---|---|---|
| ASC(character) | Returns the character code of a single character | ASC('A') | 65 |
| CHR(code) | Returns the character for a given code | CHR(65) | 'A' |
| Character | ASCII Code |
|---|---|
| 'A' | 65 |
| 'Z' | 90 |
| 'a' | 97 |
| 'z' | 122 |
| '0' | 48 |
| '9' | 57 |
| ' ' | 32 |
letter ← 'G' lower ← CHR(ASC(letter) + 32) OUTPUT lower // 'g'
INPUT char
IF ASC(char) >= 48 AND ASC(char) <= 57 THEN
OUTPUT char, " is a digit"
ELSE
OUTPUT char, " is not a digit"
ENDIF
| Function | Converts To | Example | Result |
|---|---|---|---|
| INT(value) | Integer | INT("42") | 42 |
| INT(value) | Integer (truncates) | INT(3.99) | 3 |
| FLOAT(value) | Real / Float | FLOAT("3.14") | 3.14 |
| STR(value) | String | STR(42) | "42" |
INPUT "Enter first number: ", input1 INPUT "Enter second number: ", input2 num1 ← FLOAT(input1) num2 ← FLOAT(input2) total ← num1 + num2 OUTPUT "The sum of " & input1 & " and " & input2 & " is " & STR(total)
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Not converting INPUT to number | "5" + "3" = "53" not 8 | Use INT() or FLOAT() before arithmetic |
| SUBSTRING wrong parameters | Confusing start position with end position | SUBSTRING(string, start, length) - start + length |
| POSITION returning -1 | Forgetting to handle "not found" case | Check if result = -1 before using it |
| ASC() on a string | ASC() takes a single character, not a string | Use ASC(SUBSTRING(text, 0, 1)) for first char |
| Forgetting STR() in output | Cannot concatenate string with number directly | Convert number to string with STR() first |
Q1: What does LENGTH("GCSE Computer Science") return?
Q2: Write pseudo-code that asks for a word and outputs the first three characters.
Q3: What is the ASCII code for 'A'? What is CHR(97)?
Q4: Write pseudo-code that takes two numbers as input, adds them, and outputs the result as a sentence.
Q5: Write pseudo-code that checks if the first character of a word is uppercase.
INPUT word OUTPUT SUBSTRING(word, 0, 3)
INPUT "Enter first number: ", a INPUT "Enter second number: ", b num1 ← FLOAT(a) num2 ← FLOAT(b) total ← num1 + num2 OUTPUT a & " + " & b & " = " & STR(total)
INPUT word
firstChar ← SUBSTRING(word, 0, 1)
IF ASC(firstChar) >= 65 AND ASC(firstChar) <= 90 THEN
OUTPUT "First character is uppercase"
ELSE
OUTPUT "First character is not uppercase"
ENDIF
✗ Forgetting to cast input from string to the correct data type ✓ INPUT usually returns a string; you must convert it to INTEGER or REAL before arithmetic, e.g. age ← INTEGER(INPUT('Age: ')).
✗ Thinking string indexing starts at 1 in all languages ✓ Python and most languages use 0-based indexing; the first character is at index 0. Always check the convention for your exam board.
✗ Confusing LENGTH() and SUBSTRING() functions ✓ LENGTH() returns the number of characters in a string; SUBSTRING(string, start, length) extracts a portion of the string.
✗ Not realising that string concatenation does not add spaces automatically ✓ Joining 'Hello' & 'World' gives 'HelloWorld'. You must explicitly include a space: 'Hello' & ' ' & 'World' gives 'Hello World'.
Write pseudo-code that inputs a full name (e.g. 'Jane Smith'), extracts and outputs the first name and last name separately, then outputs the initials in uppercase. [5 marks]
INPUT 'Enter full name: ', fullname space_pos ← 0 FOR i ← 0 TO LENGTH(fullname) - 1 IF fullname[i] = ' ' THEN space_pos ← i ENDIF NEXT i firstname ← SUBSTRING(fullname, 0, space_pos) lastname ← SUBSTRING(fullname, space_pos + 1, LENGTH(fullname) - space_pos - 1) OUTPUT 'First name: ' & firstname OUTPUT 'Last name: ' & lastname initials ← UPPER(firstname[0]) & '.' & UPPER(lastname[0]) & '.' OUTPUT 'Initials: ' & initials
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including input, output and string handling functions for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including input, output and string handling functions to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including input, output and string handling functions, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.