String manipulation Flashcards
Types of strings
String literal:
Characters enclosed in quotation marks
String variable:
Can be declared
e.g. password = “6G-!”
The same output can be obtained with different commands:
print(“6G-!”)
print(password)
Concatenation
Strings can be joined end to end to form larger strings using the + symbol
String traversal
- what it is
- why it’s used
- how it’s used
The process of moving through a string one character at a time
It can be used to see if one contains a particular character or group of characters
The length of the string must be found in order to create a loop to examine each character
e.g.
stringLength = myString.length
for index = 0 to stringLength - 1
character = myString(index)
print(character)
next index
The loop has to run to the length of the string minus one, as it starts at 0
Indexes of a string
The position of a character in a string is given by its index number
The first index is 0, not 1
Substrings
Substrings/portions of a string can be snipped out
e.g.
snip = myString.substring(2,4)
The first number is the starting point, and the second number is the number of characters
A student has stored their grades in a string variable named ‘grades’. e.g. ‘AcBCab’
Write an algorithm, using pseudocode, that would count the number of occurrences of a grade entered by a user. [5]
total = 0 searchGrade = input("Please enter the grade.")
for index = 0 to grades.length - 1 if searchGrade.upper == grades(index).upper then total = total + 1 end if next index
print(“There are “ + total + “ “ searchGrade + “ grades.”)
A student has stored their grades in a string variable named ‘grades’. e.g. ‘AcBCab’
Write an algorithm, using pseudocode, that would count the number of occurrences of a grade entered by a user. [5]
total = 0 searchGrade = input("Please enter the grade.")
for index = 0 to grades.length - 1 if searchGrade.upper == grades(index).upper then total = total + 1 end if next index
print(“There are “ + total + “ “ searchGrade + “ grades.”)
A student has stored their grades in a string variable named ‘grades’. e.g. ‘AcBCab’
Write an algorithm, using pseudocode, that would count the number of occurrences of a grade entered by a user. [5]
total = 0 searchGrade = input("Please enter the grade.")
for index = 0 to grades.length - 1 if searchGrade.upper == grades(index).upper then total = total + 1 end if next index
print(“There are “ + total + “ “ searchGrade + “ grades.”)
State the substring that would be printed by the following algorithm.
myString = “Computer Science”
substring = myString.substring(11,3)
print(substring)
[1]
ien