String manipulation Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Types of strings

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Concatenation

A

Strings can be joined end to end to form larger strings using the + symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String traversal

  • what it is
  • why it’s used
  • how it’s used
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Indexes of a string

A

The position of a character in a string is given by its index number

The first index is 0, not 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Substrings

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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]

A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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]

A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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]

A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

State the substring that would be printed by the following algorithm.

myString = “Computer Science”
substring = myString.substring(11,3)
print(substring)

[1]

A

ien

How well did you know this?
1
Not at all
2
3
4
5
Perfectly