Input, output and string methods

<< Previous: Relational operations Next: Random >>

Input and output

Output statements are used whenever you want to output something to the user. Either commas or plus signs can be used to join values in variables with string literals in an output statement. To get input from the user is a two step process. Step one is to output a prompt to the user so they know what to enter. Step 2 is to create a variable and assign the user input to this variable.

In general we treat plus as a join like it is literal and comma like a space will be automatically added. However its not essential to be precise here. As long as the programmer can understand what is required.

String methods

String methods are methods we can use to manpulate string values.

String method What it is/why it's needed Example
Casting Casting is when we change the data type data type of a variable. In some languages, like Python, all input will be strings unless you cast it so this will be essential to get real or integer input from the user. Some languages only allow you to concatenate strings and so to output variables with numeric values alongside strings these must be cast. STRING_TO_INT("42")
evaluates to 42
REAL_TO_STRING(3.14)
evaluates to "3.14"
Length Most languages contain a method to return the length in characters of a string. LEN("Bob")
evaluates as 3
Substring A substring is a part of an existing string. The substring method allows us to specify the character position to start and end at to create a new substring. When writing a substring in pseudocode we take the start and end values as inclusive. The actual way this works in specific languages varies. Remember also that we assume zero indexing so E is in position 0, l in postion 1 and e in position 2. SUBSTRING(2,4,"Elephant")
evaluates as "eph"
Position The position method lets us specify a character or substring and what will be returned is the position of the first occurence of the character or substring being searched for. POSITION("Elephant","p")
evaluates as 3
Concatenation This is a long word that smply means joining strigs together. If you put a plus operator between numeric data type it will add them but when you place it between forename ← "Bob"
surname ← "Dobolina"
fullname ← forename + " " + surname
fullname evaluates as "Bob Dobolina"
Character codes Most languages have a method to convert a character to it equivalent ASCII value as a decimal number and another method to convert a decimal ASCII value back to it's character equivalent. CHAR_TO_CODE("A")
evaluates as 65
CODE_TO_CHAR(65)
evaluates as "A"

Knowledge check


Questions:
Correct:

Question text


<< Previous: Relational operations Next: Random >>

© All materials created by and copyright S.Goff