Algorithm - Strings
Strings are generally a sequence of characters (letters and numbers). They can be thought of as character arrays, but in most programming languages are their own variable type.
Extracting data from a string
Most languages offer the facility of extracting data from strings. Depending on the language used, the verbs can differ. Examples of typical verbs are mid$ or instr$
In the algorithms that follow, a general statement is used: extract from the ith character (for n characters) from String into ExtractedString.
To extract the days and months from a date in the format DD/MM/YY the following algorithm is relevant:
Note: We need to pull out the first 2 characters and place them in Days, and the 4th and 5th characters and place them into Month.
BEGIN FindDaysandMonths
Get DateString
Let StartDays = 1
Let StartMonths = 4
‘the fourth character in the date string is the start of the month value
Extract from the StartDaysth character (for 2 characters) from DateString into Days
Extract from the StartMonthsth character (for 2 characters) from DateString into Month
Display“the month is ” Month “ and the day of the month is ” Days
END FindDaysandMonths
Insertion of a string into another string
To insert a string into a second string, the best way is to concatenate the required strings together, as not all languages support the insertion of a string into second string with a single command.
The following example looks for the first occurrence of the delimiter “ ; ” in a string. It splits the string into two parts – the characters preceding the delimiter, and the characters following the delimiter. It then effectively inserts an entered word at that position in the existing string by joining all three parts together:
BEGIN InsertNewWordintoString
Get InitialString, NewWord
Let L = Length of InitialString
Let Found = 0
Let i = 1
REPEAT
extract from the ith character from InitialString into CheckLetter
IF CheckLetter = “ ; ” THEN
extract from the 1st character (for i – 1 characters) from InitialString into FirstPart
extract from the (i + 1)th character (for L – i characters) from InitialString into SecondPart
Let NewString = FirstPart + NewWord + SecondPart
‘note that if we use the + operator with strings, the values are concatenated
found = 1
END IF
i = i + 1
UNTIL i > = L OR found = 1
IF found = 0 THEN
Display “the delimiter ; could not be found in your string”
ELSE
Display “The new string is ” NewString
ENDIF
END InsertNewWordintoString
Deletion of a string from another string
To delete a string from a second string, the best way is to concatenate two partial strings together.
The following example looks for the first occurrence of an entered word in a string, and deletes that word by effectively joining the first and last parts of the original string together:
BEGIN DeleteWordFromString
Get InitialString, StringToGo
Let LString = Length of InitialString
Let Lword = Length of StringToGo
Let found = 0
Let i = 0
REPEAT
extract from the ith character (for Lword letters) from InitialString into CheckforWord
IF CheckforWord = StringToGo THEN
extract from the 1st character (for i – 1 characters) from InitialString into FirstPart
extract from the (i + Lword)th character (for (LString – Lword – i + 1) characters) from InitialString into SecondPart
Let NewString = FirstPart + SecondPart
found = 1
END IF
i = i + 1
UNTIL i > = LString OR found = 1
IF found = 0 THEN
Display “The word could not be found in your string”
ELSE
Display “The new string is ” NewString
ENDIF
END DeleteWordFromString