{The index to all of Carl Drott's Pascal examples is at:} { http://drott.cis.drexel.edu/CommandFinder.html } {NOTE: If you are looking for a chart of ASCII codes, look at} {http://drott.cis.drexel.edu/I101/asciichart.GIF} {all characters are represented by numbers. The standard code that specifies} {what number represents what character is named ASCII} {} {the ORD command takes a character and gives back the corresponding} {numeric code} program aug6no1; var mychar: char; mynum: integer; begin mychar := 'a'; while mychar <> 'Z' do begin readln(mychar); mynum := ord(mychar); writeln('The ASCII code for ', mychar, ' is ', mynum); end; end. {The ability to manipulate the ASCII code numbers lets us change the case} {of letters. If you run the program above you will see that A has a code of 65} {while a has a code of 97. In fact, every lower case letter has a value that is 32} {more than its upper case form} {} {The CHR command takes an integer and turns it into the corresponding character} program aug6no2; var mychar: char; mynum: integer; begin mychar := 'a'; while mychar <> 'Z' do begin readln(mychar); mynum := ord(mychar); writeln('The lowercase for ', mychar, ' is ', chr(mynum + 32)); end; end. {Try the program above on a number, punctuation mark or lower case letter.} {The result is nonsense. We only want to lower case characters that start as upper case } {letters} {} {The following program adds an IF-THEN to check for this} program aug6no3; var mychar: char; mynum: integer; begin mychar := 'a'; while mychar <> 'Z' do begin readln(mychar); mynum := ord(mychar); if mychar in ['A'..'Z'] then writeln('The lowercase for ', mychar, ' is ', chr(mynum + 32)) else writeln(mychar, ' is not uppercase, dumbo!'); end; end. {OK -- if we add 32 to lower case a letter, how do we make one upper case?} {} {Modify the example ajbove to do this -- don't forget to check that the} {letter is lower case to start with} program aug6no4; var mychar: char; mynum: integer; begin mychar := 'a'; while mychar <> 'Z' do begin readln(mychar); mynum := ord(mychar); if mychar in ['a'..'z'] then writeln('The uppercase for ', mychar, ' is ', chr(mynum - 32)) else writeln(mychar, ' is not lowercase, dumbo!'); end; end. {The for loop below generates a table of ASCII values -- can you see how?} {} {When we did this on a Mac we saw that character 169 was the copyright sign} {(it won't be on a PC)} {The program shows how we can use the code number to print a character that} {there is no key for -- this is especially nice for something like a } {tab -- code 9} program aug6no5; var mychar, cmark: char; mynum: integer; begin cmark := chr(169); for mynum := 0 to 255 do begin writeln(mynum, ' ', chr(mynum)); end; writeln('This wisdom is ', cmark, ' 1996 by C. Drott '); end. {The program below shows how we can check the number that the user enters} {Note that if you change the first readln to } {readln(mynum)} {and the user types a letter, the program quits (or at least doesn't tell} {the user what is wrong)} {reading the input as a character, which can hold any keystroke, lets us} {generate a message} program aug6no6; var mychar, cmark: char; mynum: integer; begin writeln('enter a number between 0 and 9'); readln(mychar); while not (mychar in ['0'..'9']) do begin writeln(mychar, ' isnot a number between 0 and 9'); writeln('tryagain'); readln(mychar); end; end. {Reading the input as a character above solves one problem but creates another.} {The problem is that you can't do arithmetic on characters -- even} {if the characters are digits!} {Look how we use ord to convert the character to a number} {Hint: the ord of zero is 48} program aug6no7; var mychar, cmark: char; mynum, sum: integer; begin writeln('enter a number between 0 and 9'); readln(mychar); while not (mychar in ['0'..'9']) do begin writeln(mychar, ' isnot a number between 0 and 9'); writeln('tryagain'); readln(mychar); end; mynum := ord(mychar) - 48; sum := 2 * mynum; writeln(sum : 1, ' is double your number'); end. {Here we go the other way, converting a number to its equivalent character so we} {can store it as part of a string} program aug6no8; var mychar, cmark: char; callit: string; mynum, sum: integer; begin mynum := 1; while mynum < 10 do begin mychar := chr(mynum + 48); callit := concat('aug6no', mychar); writeln('call the file ', callit); mynum := mynum + 1; end; {the last sample program} program search1; var whichletter, whatvowel: integer; myline, vowels: string; isgood: boolean; theinfile, theoutfile: text; begin vowels := 'aeiou '; reset(theinfile, 'phrases'); rewrite(theoutfile, 'output phrases'); while not eof(theinfile) do begin readln(theinfile, myline); whichletter := 1; whatvowel := 1; isgood := true; while (whichletter <= length(myline)) and (isgood) do begin if myline[whichletter] in ['a', 'e', 'i', 'o', 'u'] then begin {letter is a vowel} if myline[whichletter] = vowels[whatvowel] then whatvowel := whatvowel + 1 else isgood := false; end;{letter is a vowel} whichletter := whichletter + 1; end;{whichletter <= length} if (isgood) and (whatvowel = 6) then writeln(theoutfile, myline); end;{ not eof} end.{end of the program} end.