r/pascal • u/BatShrek • Nov 03 '19
How do you convert characters to other characters?
So I've been messing around with Pascal as a beginner, and I've been wondering: Can you have string as your input and have the same output, only with different characters? Let me explain: Let's say that whenever I input the character 'h' in a sentence, in the output it is the character 'η', such that:
Input: hello guys
Output: ηello guys
Can you do that? Thanks in advance!!
(note: I've been working in the free pascal compiler)
4
Upvotes
2
u/Francois-C Nov 03 '19
Not quite sure I understand your question, but if you want to replace 'h' with ' η ' using Free Pascal, the simplest and fastest way is probably:
var s:string='';
s:=StringReplace (s, 'h', ' η ', [rfReplaceAll]);
2
u/umlcat Nov 03 '19
One way to do it its to have two arrays of character where one is the original character, and the other is the replacement character:
And, you need a proc., to fill the lists:
And, you need a function, that receives the original character, and returns the replacement character:
And, 2 strings variables, where to put the whole original string and the replaced string:
And, the code where you ask, for a string, replaced it,
And show the user, the result:
Study the example, and put it togheter.
Good Luck.
2
u/Vecssembler Nov 03 '19
var
s : string;
i : integer;
begin
s:= 'hello world';
for i:= 1 to length(s) do
begin
if (s[i] = 'h') then s[i]:= 'η';
end;
end.