Solved

question about chr

Posted on 1998-11-26
6
205 Views
Last Modified: 2010-04-04
I am using Delphi 3 and have 2 edit boxes on the screen and 1 button.

In 1 edit box, I type lower case alphabet only words.  When I press the button, I take each character 1 at a time, and add 1 to the ordinal value of the character.  (Up to this point, this works fine)

Then I want to store each 'new' character into a string variable with a statement like this:  var output:string;
                                          input:string;
                                          x:integer;
  x:=ord(input[i]);
  x:=x+1;
  output:=chr(x);  <-- this is the line that the compiler keeps on saying that there is an error.
What am I doing wrong?

Thanks

0
Comment
Question by:psabourin
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1348167
The problem is that you are trying to store a char in a string variable.  These are two different types.

One solution would be to use concatenation.

x:=ord(input[i]);
  x:=x+1;
  output := '';
  output := output + chr(x);  


Or you could set one of the chars in the string directly.

Like this:

x := ord(input[i]);
  SetLength(s,1);
  s[1] :=chr(x);


I don't know what exactly your purpose is, but I would suggest something like this:

output := '';
for i := 1 to length(input) do begin
  x:=ord(input[i]);
  x:=x+1; if x>255 then x := 0;
  output := output + chr(x);
end;

Output will be contain a string of characters, all 1 greater than the corresponding character in input.


0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1348168
Or, even better, you could possibly do this:

output := '';
for i := 1 to length(input) do
  output := output + succ(input[i]);

where you can avoid changing to an integer altogether.
(succ is the successor function, an ANSI pascal function.  Check your help file on it for more details.)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348169
hi psabourin,

your OutputVar is a string and you will place there a Char, thats incompatibel.

When the Output holds only a Char you can use

Output := '';
Output := Output + Chr(X);

When Output holds the InputString manipulated each Char Increased by one then
only

Output := Output + Chr(X);

To manipilulate the whole InputString to OutputString
Use

Output := '';
For I := 1 to Length(Input) do Output := Output + Chr(Ord(Input[I])+1);


Meikl

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:psabourin
ID: 1348170
Thanks
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1348171
wow, this q was from nov '98
0
 

Author Comment

by:psabourin
ID: 1348172
Sorry, got tied up with other stuff.  I meant to do this a long time ago.  But better late than never, I suppose!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question