next up previous
Next: Part B - Adding Up: No Title Previous: No Title

Part A - Manipulating strings

  1. Consider the following very simple program that makes a copy of an input string and then outputs both the original string and the copy.

    var inputString: string
    var copy: string
    put "Enter a string.  " ..
    get inputString
    copy := inputString
    put "Original string is ", "\"", inputString, "\""
    put "Copied string is   ", "\"", copy, "\""

    Try running this program with various input.

  2. The following program is meant to take the same input and produce the same output as the program in (1). Instead of using a simple assignment statement to make a copy (line 5 in the original), it builds up the copy by appending character at a time.

    var inputString: string
    var copy: string
    put "Enter a string.  "  ...
    get inputString
    for i: 1 .. length(inputString)
        copy := copy + inputString(i)
    end for
    put "Original string is ", "\"", inputString, "\""
    put "Copied string is  ", "\"", copy, "\""

    The program given here contains two errors. Correct these and then test the program to make sure its input/output behavior is the same as that of the first program.

  3. One advantage of making the copy in this second way is that it lets us make a changed or edited copy. For example, to make a copy with all the blanks squeezed out, we would replace the body of the counted loop as follows:

    for i: 1 .. length(inputString)
      if inputString(i) not= " " then
        copy := copy + inputString(i)
      end if
    end for

    A run of this program should then look something like this:

    Enter a string.   "Here   is   a   test string." 
    Original string is "Here   is   a   test string."
    Copied string is  "Hereisateststring."

    Try writing different versions of this program to do the following:

    1. Produce a copy with all occurrences of the letter ``a" changed to a capital ``A", but with no other changes. Sample run:

      Enter a string.   "Alaska is large." 
      Original string is "Alaska is large."
      Copied string is  "AlAskA is lArge."
    2. Produce a backwards copy. Sample run:

       	
      Enter a string.   "Here   is   a   test string." 
      Original string is "Here   is   a   test string."
      Copied string is  ".gnirts tset   a    si    ereH."
    3. Produce a copy where the second or additional occurrence of any character is omitted.
      Sample run:

       	
      Enter a string.   "Here   is   a   test string." 
      Original string is "Here   is   a   test string."
      Copied string is  "Her isatng."
    4. Produce a copy where all small letters (``a"..``z") are changed to the corresponding capitals (``A"..``Z"), but with no other changes. Sample run:

       	
      			Enter a string.   "Here   is   a   test string." 
      			Original string is "Here   is   a   test string."
      			Copied string is  "HERE  IS   A  TEST STRING."



Michelle Wahl Craig
Tue Feb 20 14:55:15 EST 1996