Solved

question about chr

Posted on 1998-11-26
6
201 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now