TOPIC C: Characters
Contents :
CHAR, RdChar(), WrChar(), string,
WrStr(), RdStr().
Exercises :
D1: From character to string.
Copy the solution (see blue Solution icon).
- Define a character variable a. Ask the user for a character.
- Define a word (string) as an array of 10 characters. Ask the
user for a word.
- If the second character of the word is a vowel, change it to the
given character (variable a).
- Print out the array, letter by letter.
S1: Search & Replace.
- 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).
- 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
- Decypher the above sequence of numbers (copy the code below into
your program):
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
- write a program that codes a given sentence with the code of H2.
- for die-hards (much more difficult): write a program
that codes a given sentence with the code of S2.
- hint: use the solution of H2/S2 to test your coder
H1: Fill.
- Ask the user for a letter until he enters the letter 'o'.
- put a RdLn();
after the RdChar();
(see for explanation)
- Fill an array of size 20 repetively with the letters, until the
array is full.
- Print out the resulting string.
- eg: user enters 'a', 'b', 'c', 'o' =>
output is "abcabcabcabcabcabcab".
- Remark: the shortest solution contains 8 lines of
statements (1 statement per line, only multiple read & write
statements are allowed on the same line).
H2: Cryptography III (geheimschrift).
This one is easier than than S2
- Decypher the next sequence of numbers (copy the code below into
your program)
- code[1] := 13; code[2] := -5; code[3] := 31; code[4] :=
-8; code[5] := 12;
code[6] := -9; code[7] := 22; code[8] := -14; code[9] := 39;
code[10] := -20;
code[11] := 23; code[12] := 6; code[13] := 26; code[14] := -20;
code[15] := 29;
code[16] := -22; code[17] := 54; code[18] := -34; code[19] := 35;
code[20] := -22;
code[21] := 25; code[22] := 7; code[23] := -6; code[24] := 38;
code[25] := -34;
code[26] := 37; code[27] := -36; code[28] := 55; code[29] := -23;
code[30] := 43;
code[31] := -34; code[32] := 47; code[33] := -40; code[34] := 59;
code[35] := -58;
code[36] := 89; code[37] := -57; code[38] := 67; code[39] := -63;
code[40] := 72;
code[41] := -57; code[42] := 61; code[43] := -60; code[44] := 80;
code[45] := -50;
code[46] := 82; code[47] := -76; code[48] := 85; code[49] := -78;
code[50] := 110;
code[51] := -109; code[52] := 113; code[53] := -110; code[54] :=
142; code[55] := -128;
code[56] := 129; code[57] := -103; code[58] := 108; code[59] :=
-107; code[60] := 119;
code[61] := -114; code[62] := 122; code[63] := -99; code[64] :=
129;
- with the following code:
- each number of the sequence except the first (!) corresponds
to a character, the transformed sequence results in a sentence.
- decodeArray is the key table for the decoding...
- decodeArray := "azertyuiopqsdfghjklmwxcvbna.,!2
:;";
- the corresponding character c of a number x is calculated as
follow:
- sum x with the previous number of the sequence, this sum
gives you the index in the decodeArray of the character c.
- Hints:
- Thus the first letter is a 'i', because code[2] (= 13) + the
previous code[1] (= -5) gives 8, and the 8th letter in de decodeArray
is a 'i'.
- You'll have 3 arrays:
- code (array of cardinals) with the sequence of
numbers to decypher
- decodeArray (array of 34 chars) with the decode
key.
- result (array of chars): here you put the decoded
letters (one for each number of code, minus the first)
T1: Output
- What is the output of the following program? Calculate in it on
paper!!
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.
- There are 2 errors in the unreadable result sentence. Try to
correct these to get a readable sentence, only 2 numbers has to be
changed in the above code. Run the code to test if it works!
T2: Output 2
- What is the output of the following program? Calculate in it on
paper!!
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.
- What is the output of the following program? Calculate in it on
paper.
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.
- Op de 2 lijnen met (* xx *) wil ik in feite de
waarden van arr1[i] en arr2[i] onderling
verwisselen. Wat moet ik
aan de code veranderen om dit te verkrijgen