Programming Fundamentals/Strings/Pseudocode

strings.txt

edit

Main

edit
Declare String Name
Declare Integer Index
Declare String Last
Declare String First

=== Find ===
<pre>
// Returns the 0-based index of Substring in Text, or -1 if not found.
Declare Integer Index
Declare Boolean Done

Set Index = 0
Set Done = false
Do
    If Index + len(Substring) <= len(Text) Then
        If Substring(Text, Index, len(Substring)) == Substring Then
            Set Done = true
        Else
            Set Index = Index + 1
        End If
    Else
        Set Index = -1
        Set Done = true
    End If
While Not Done

Return Index

Lower

edit
// Returns Text converted to lower case.
Declare Integer Index
Declare Integer Code

For Index = 0 To len(Text) - 1
    Set Code = toCode(substring(Text, Index, 1))
    If Code >= 65 AND Code <= 90 Then
        Set Code = Code + 32
        Set Text = Substring(Text, 0, Index) + toChar(Code) + Substring(Text, Index + 1, len(Text) - Index - 1)
    End If
End For

Return Text

Substring

edit
// Returns the substring of Text beginning at Start up to the given Length.
Declare String Result
Declare Integer Index

Set Result = ""
Set Index = Start
While Index < len(Text) AND Length > 0
    Set Result = Result + substring(Text, Index, 1)
    Set Index = Index + 1
    Set Length = Length - 1
End While

Return Result

Trim

edit
// Returns Text with leading and trailing spaces removed.
Declare Boolean Done

Set Done = false
Do
    If len(Text) > 0 Then
        If substring(Text, 0, 1) == " " Then
            Set Text = Substring(Text, 1, len(Text) - 1)
        Else
            Set Done = true
        End If
    Else
        Set Done = true
    End If
While Not Done
Set Done = false
Do
    If len(Text) > 0 Then
        If substring(Text, len(Text) - 1, 1) == " " Then
            Set Text = Substring(Text, 0, len(Text) - 1)
        Else
            Set Done = true
        End If
    Else
        Set Done = true
    End If
While Not Done

Return Text

Upper

edit
// Returns Text converted to upper case.
Declare Integer Index
Declare Integer Code

For Index = 0 To len(Text) - 1
    Set Code = toCode(substring(Text, Index, 1))
    If Code >= 97 AND Code <= 122 Then
        Set Code = Code - 32
        Set Text = Substring(Text, 0, Index) + toChar(Code) + Substring(Text, Index + 1, len(Text) - Index - 1)
    End If
End For

Return Text

See Also

edit