?
Solved

Words spelled the same just looking different .

Posted on 2003-03-07
7
Medium Priority
?
148 Views
Last Modified: 2010-04-04
Hi All,

This is not homework. I need an efficient way to make this happen

I have a word ...... any of any two or more letters,  for instance 'Td'. It can be entered in one of 2 ways.
 
If the word begins with a capital letter, then the  revised word should begin with a lower case letter and have the same spelling.  User enters 'Td' in Edit1 and does a button click and in Edit2 shows  'td'
OR
if the word begins with a lower case letter then the word revised should begin with a capital letter and have the same spelling.  User enters 'td' in Edit1 and does a button click and in Edit2 shows  'Td'

Delphi3
0
Comment
Question by:delphi3
[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
7 Comments
 
LVL 6

Accepted Solution

by:
zebada earned 200 total points
ID: 8093303
if ( Length(Edit1.Text)>1 ) then
begin
  if ( Edit1.Text[1] in ['A'..'Z'] ) then
    Edit2.Text := LowerCase(Edit1.Text[1])+Copy(Edit1.Text,2,MAXINT)
  else if ( Edit1.Text[1] in ['a'..'z'] ) then
    Edit2.Text := UpperCase(Edit1.Text[1])+Copy(Edit1.Text,2,MAXINT)
  else
    Edit2.Text := Edit1.Text;
end
else
  Edit2.Text := Edit1.Text;



0
 
LVL 6

Expert Comment

by:DaFox
ID: 8093676
Hi.

procedure TForm1.Button1Click(Sender: TObject);
var
  c: Char;
begin
  if (Edit1.Text <> '') then
  begin
    c := #0;
    // word starting with capital letter
    if Ord(Edit1.Text[1]) in [65..90] then
    begin
      c := Chr(Ord(Edit1.Text[1]) + 32);
    end;
    // word starting with small letter
    if Ord(Edit1.Text[1]) in [97..122] then
    begin
      c := Chr(Ord(Edit1.Text[1]) - 32);
    end;
    Edit2.Text := c + Copy(Edit1.Text, 2, Length(Edit1.Text) - 1);
  end;
end;

Markus
0
 
LVL 1

Expert Comment

by:KillerApp
ID: 8094152
Edit2.Text = Chr(Ord(Edit1.Text[1]) xor 32) + Copy(Edit2.Text,2,MAXINT);
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!

 
LVL 6

Expert Comment

by:DaFox
ID: 8094194
KillerApp, not bad!!! ;-)
That's what I wanted to do with ascii/ord/chr, but forgot what I wanted to do with it when I open up my Delphi ;-)

Unfortunately in this case you got no security checks. That means e.g. $oft is getting oft and so on, if you'd do it my way, only letters get inverted.

->   Edit2.Text := Chr(Ord(Edit1.Text[1]) xor 32) + Copy(Edit1.Text, 2, Length(Edit1.Text) - 1);

Markus
0
 
LVL 1

Expert Comment

by:KillerApp
ID: 8094289
>Unfortunately in this case you got no security checks.


That is true but the question said "I have a word ...... any of any two or more letters", so my answer is still valid :P

A secure version

if Length(Edit1.Text)>0 AND (Edit1.Text[1] in ['a'..'z','A'..'Z']) then
  Edit2.Text := Chr(Ord(Edit1.Text[1]) xor 32) + Copy(Edit1.Text, 2, Length(Edit1.Text) - 1);
0
 
LVL 4

Author Comment

by:delphi3
ID: 8095541
Thanks to you all for responding.

KillerApp, the secure version that you wrote needed some brackets more()'s after if(.... ) AND (.....)then ... to make it work in my pascal.
My revision would be:  
if (Length(Edit1.Text)>0) AND (Edit1.Text[1] in ['a'..'z','A'..'Z']) then
 Edit2.Text := Chr(Ord(Edit1.Text[1]) xor 32) + Copy(Edit1.Text, 2, Length(Edit1.Text) - 1);


zebada was in with a workable solution first.

Thanks again to all.
Delphi3








0
 
LVL 1

Expert Comment

by:KillerApp
ID: 8097307
Yes you are right, my mistake. I am always doing that! (in C++ and VB  the brackets would not be needed).
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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