Solved

question about chr

Posted on 1998-11-26
6
208 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 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
Technology Partners: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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