TOPIC C: Characters

Contents :

  CHAR, RdChar(), WrChar(), string, WrStr(), RdStr().

Exercises :


D1: From character to string.

Copy the solution (see blue Solution icon).

S1: Search & Replace.

  1. Ask the user for a word (of maximal 30 characters) and a letter. Search for the letter in the word and print out the index's when found. Also print the number of appearances (het aantal keren dat de letter voorkomt).
  2. Replace in the word the given letter by a 'z'. Print out the resulting word.

S2: Cryptography (geheimschrift)

20 24 93 20 63 66 75 32 60 19 32 24 32 82 70 36 93 38 66 19 42  47 40 52 24 31 66 41 37 42 14 70 10  19 53 36 82 30     code[1] := 20; code[2] := 24; code[3] := 93; code[4] := 20; code[5] := 63;
    code[6] := 66; code[7] := 75; code[8] := 32; code[9] := 60; code[10] := 19;
    code[11] := 32; code[12] := 24; code[13] := 32; code[14] := 82; code[15] := 52;
    code[16] := 36; code[17] := 93; code[18] := 38; code[19] := 66; code[20] := 19;
    code[21] := 42; code[22] := 46; code[23] := 6; code[24] := 52; code[25] := 24;
    code[26] := 31; code[27] := 66; code[28] := 41; code[29] := 37; code[30] := 42;
    code[31] := 14; code[32] := 70; code[33] := 12; code[34] := 19; code[35] := 53;
    code[36] := 36; code[37] := 70; code[38] := 30;

S3: Palindrome.

Ask the user for a word (of maximal 30 characters) and check if it is a palindrome, ie. you can read the same word in opposite order (van achter naar voor heb je hetzelde woord).
eg: You have to know that the end of a word in a string array is indicated by a CHR(0). This is necessary for calculating the length of the word.
Check also words that are almost palindromes.
 
 


X1: CryptographyII



H1: Fill.



H2: Cryptography III (geheimschrift).

This one is easier than than S2



T1: Output

MODULE T1;
<* NOOPTIMIZE + *>
    FROM IO IMPORT RdChar, WrChar, WrStr, RdStr, WrLn, RdCard, WrCard, RdInt, WrInt;     (* import general procedures *)

    CONST STRING_SIZE = 30;          (* constants *)

    VAR
      str, strCopy: ARRAY[1..STRING_SIZE] OF CHAR;            (* variable-declarations *)
      i:CARDINAL;
BEGIN
    WrLn;
    str := "this is Modula2!";

    FOR i := 1 TO 4 DO
      strCopy[i] := str[i+5];
      str[i+3] := str[i];
    END;

    i := 1;
    REPEAT
      IF i < 3 THEN
        str[i] := strCopy[i];
      END;
      i := i + 1;
    UNTIL (str[i] = '!');
    str[i] := "?";
    WrStr(str);
    WrLn;

END T1.


T2: Output 2

MODULE T2;
<* NOOPTIMIZE + *>
    FROM IO IMPORT RdChar, WrChar, WrStr, RdStr, WrLn, RdCard, WrCard, RdInt, WrInt;     (* import general procedures *)

    CONST STRING_SIZE = 30;          (* constants *)

    VAR
      arr1, arr2, arr3: ARRAY[1..STRING_SIZE] OF CHAR;            (* variable-declarations *)
      i, j, k:CARDINAL;
BEGIN
    WrLn;
    arr1 := "Dat is er ene!";
    arr2 := "Wttwnljp zhgn?";

    k := 1;
    FOR i := 1 TO STRING_SIZE DO
      IF (arr1[i] = 'a') OR (arr1[i] = 'e') OR (arr1[i] = 'i')  OR (arr1[i] = 'u')  OR (arr1[i] = 'o') THEN
        FOR j := i-1 TO i+1 DO
          IF j # i THEN (* # betekent verschillend dan*)
            arr3[k] := arr2[j];
          ELSE
            arr3[k] := arr1[j];
          END;
          k:=k+1;
        END;
      END;
    END;

   WrStr("arr1 = ");WrStr(arr1);WrLn;
   WrStr("arr2 = ");WrStr(arr2);WrLn;
   WrStr("arr3 = ");WrStr(arr3);WrLn;
END T2.
MODULE T2b;
<* NOOPTIMIZE + *>
    FROM IO IMPORT RdChar, WrChar, WrStr, RdStr, WrLn, RdCard, WrCard, RdInt, WrInt;     (* import general procedures *)

    CONST STRING_SIZE = 30;          (* constants *)

    VAR
      arr1, arr2, arr3: ARRAY[1..STRING_SIZE] OF CHAR;            (* variable-declarations *)
      i, j, k:CARDINAL;
BEGIN
    WrLn;
    arr1 := "Dat is er ene!";
    arr2 := "Wttwnljp zhgn?";

    k := 1;
    FOR i := 1 TO STRING_SIZE DO
      IF (arr1[i] = 'a') OR (arr1[i] = 'e') OR (arr1[i] = 'i')  OR (arr1[i] = 'u')  OR (arr1[i] = 'o') THEN
        arr1[i] := arr2[i];  (* xx *)
        arr2[i] := arr1[i];  (* xx *)
      END;
    END;

   WrStr("arr1 = ");WrStr(arr1);WrLn;
   WrStr("arr2 = ");WrStr(arr2);WrLn;
   WrStr("arr3 = ");WrStr(arr3);WrLn;
END T2b.