• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

question about chr

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
psabourin
Asked:
psabourin
  • 2
  • 2
  • 2
1 Solution
 
scrapdogCommented:
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
 
scrapdogCommented:
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
 
kretzschmarCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
psabourinAuthor Commented:
Thanks
0
 
kretzschmarCommented:
wow, this q was from nov '98
0
 
psabourinAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now