Algorithms - EduqasString handling

Algorithms are step-by-step plans for solving problems. They are a starting point when writing a program. Algorithms can be designed using pseudo-code and flowcharts.

Part ofComputer ScienceUnderstanding Computer Science

String handling

There are several built-in functions in most . They do things like calculate the length of a string or extract a part of a string. Since they are so common, they are also included in and the functions in this section use the Eduqas pseudocode versions.

String passing

As in other functions, a string function will typically expect a string value as an argument to the function, then return the result to the calling program.

User-defined functions can also be written to process strings, passing string values for purposes not covered by the built-in functions.

Concatenation

Concatenation means joining two strings together. In the following program, three are defined as strings and the firstname and surname values are set. The final variable, fullname, is created by joining the firstname and surname values together with a space between the strings:

firstname is string surname is string fullname is string set firstname = “Bob” set surname = “Smith” set fullname = firstname & " " & surname

Finding the length of a string

To find the length of a string the function len is used.

word is string set word = “Computer Science” output len(word)

The output in this example would be 16 because it includes the space between the words.

String comparison

There are two main ways of comparing strings of :

  • checking if there is an exact content match
  • comparing if there is an exact content match but ignoring differences in upper and lower case letters

The way to do this is using the strComp function:

strComp(string1, string2)

Checking if there is an exact match

In this example, two words are compared. If they exactly match then the program displays “They match”. Otherwise it displays the message “No match”:

word1 is string word2 is string set word1 = “Computer Science” set word2 = “Computing” if strComp(word1,word2) output “They match” else output “No match” end if

Ignoring differences in case

To compare if there is an exact content match, ignoring differences in upper and lower case letters requires the use of another parameter, namely adding a ‘1’:

Name1 is string Name2 is string Name1 = “Geri” Name2 = “geri” If strComp(Name1,Name2,1) output “The content matches” else output “The content doesn’t match” end if

Selecting all or part of a string

The mid function is used to find all or part of a string:

Mid(string,startpoint,endpoint) word is string part is string set word = “Computer Science” part = mid(word,10,16) output part

The output would be “Science”.

Trimming a string

Trimming a string allows additional spaces at the start or end of a string to be removed:

word is string set word = “ Computer ” newWord = trim(word)

Replacing part of a string

To replace part of a string the replace function is used:

Replace(string, find, replacement) UnhealthyPhrase is string HealthyPhrase is string Set UnhealthyPhrase = “How many sweets do you want?” HealthyPhrase = replace(UnhealthyPhrase, “sweets”, “pieces of fruit”) Output HealthyPhrase

The output would be “How many pieces of fruit do you want?”