?
Solved

question about chr

Posted on 1998-11-26
6
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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