Recent

Author Topic: Swap variables  (Read 954 times)

guest64887

  • Guest
Swap variables
« on: October 16, 2019, 08:02:56 am »
Hi, I have a question about Pascal. The main aim is that user will write his name and surname and I should swap these two positions. Can you help me how to do this?

mangakissa

  • Hero Member
  • *****
  • Posts: 942
Re: Swap variables
« Reply #1 on: October 16, 2019, 08:20:23 am »
What do you have in code?
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #2 on: October 16, 2019, 08:34:49 am »
Good question! and the answer is rather strange but true and this does not often happen:

That is not as easy as you might think, e.g. my full name is
Thaddeus Franciscus Maria De Koning. Now what is my surname and what part is my name?
Or even worse, my alter ego (an anagram for Thaddy de Koning):
Todd Hayden King. Hayden can be both surname or part of the name.
Therefor you would want to have the user to enter name and surname in different fields.
Basically name and surname can not be computed accurately. Humans often can, but even humans can not do that always correct (e.g. Hayden above).
A well written application has at least fields for name, prefix, suffix and surname, where prefix and suffix are optional.
You may also want fields for prefixed and suffixed titles, like Dr, subs. Ph.D, LLC etc.
I hope this example helps. Only the user can do this accurately.

You asked a simple question for which the answer is that there is no computable solution, not even with AI... Shit happens... 8) 8-)

Actually, that is funny, isn't it?

If the question would be reduced to swapping two strings the answer would be very easy.
Note that many "programmers" think they can solve this paradox, but that eventually always leads to data corruption.

Now you can recognize such folly... ;D Use multiple fields! And this is one example where a programmer must trust the user!

You've made my day to be able to explain this to some extend!
Thanks for the good question. :)
« Last Edit: October 16, 2019, 09:30:35 am by Thaddy »
also related to equus asinus.

trev

  • Full Member
  • ***
  • Posts: 220
  • Former Delphi 7 and Delphi 10.2 User
Re: Swap variables
« Reply #3 on: October 16, 2019, 09:46:38 am »
A well written application has at least fields for name, prefix, suffix and surname, where prefix and suffix are optional.

Prefix, First name, Middle name(s) [optional], Surname is what I've done in corporate commercial applications in the past when I was working for the man. Never been a problem.
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #4 on: October 16, 2019, 09:53:16 am »
Yes, usually sufficient (but not always! *). But the question was about separating name and surname from a single string and that is impossible.
It can not be computed, which does not always happen.... :P

* e.g. the Dutch government  software (ALL OF IT!)  is not able to handle names from e.g. Icelandic or Lithuanian languages correctly to determine family relations:
Thorsdottir (daughter of Thor)(I can complicate that even further)
Kaslauskaite (daughter of Kaslauskas) (I can complicate that even much  further: *ciute instead if *Kaite depending on name)
Svolkiene (wife of Svolka)

(And in old middle Dutch names like Janz or Janssen, which mean both son of Jan. In Dutch it has no meaning anymore)

This leads in practice to some very serious issues and this is just demonstrating European language issues that a programmer should deal with.

The point is that names and surnames play an important part in daily life and if the software is not written correctly all kinds of mishaps happen.
I am now very serious.
« Last Edit: October 16, 2019, 04:06:29 pm by Thaddy »
also related to equus asinus.

guest64887

  • Guest
Re: Swap variables
« Reply #5 on: October 16, 2019, 06:11:39 pm »
I need to use some pos or maybe insert

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #6 on: October 16, 2019, 06:13:51 pm »
I need to use some pos or maybe insert
No. it is  not possible....

If it is homework copy my answer and you have my blessing to claim it yourself.

As I wrote: switching strings is easy.
« Last Edit: October 16, 2019, 06:35:14 pm by Thaddy »
also related to equus asinus.

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #7 on: October 16, 2019, 06:24:59 pm »
End of discussion: I am not talking to apes. (I am talking to dogs, spiders, mice, daughters, friends, .... and wife only) <you are rewarded a grumpy  >:D >
also related to equus asinus.

guest64887

  • Guest
Re: Swap variables
« Reply #8 on: October 16, 2019, 06:25:06 pm »
I need to use some pos or maybe insert
No. it is  not possible....

If it is homework copy my answer and you have my blessing ti claim it yourself.

As I wrote: switching strings is easy.

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #9 on: October 16, 2019, 06:30:33 pm »
Sigh:
Code: Pascal  [Select]
  1. procedure switch(var  a, b:string):
  2. var t:string;
  3. begin
  4. t := b;
  5. b:= a;
  6. a:= t;
  7. end;
But you have no clue about the consequences of your original question.......
The question you asked is not computable. Period.
(and I edited my answer from function to procedure on purpose)
« Last Edit: October 16, 2019, 07:28:10 pm by Thaddy »
also related to equus asinus.

winni

  • Sr. Member
  • ****
  • Posts: 416
Re: Swap variables
« Reply #10 on: October 16, 2019, 06:30:38 pm »
Homework as I thought. Has nothing to do with real live.

Code: Pascal  [Select]
  1. var name1,name2,surname,name : string;
  2.      space: integer;
  3. begin
  4.   clrscr;
  5.    write(' what is your name?')
  6.    readln(name)
  7.    space:=pos(' ',name);
  8.    name1:=copy(name, 1, space-1);
  9.    delete (name,1,space);
  10.  
  11.    space:=pos(' ',name);
  12.    name2 := copy(name,1,space-1);
  13.    delete (name,1,space);
  14.  
  15.    surname := name;
  16.    writeln (surname,' ',name2,' ', name1);
  17. end;
  18.  
  19.  

results in
Code: Text  [Select]
  1.  'teacher idiotic the'

Got it?

Winni
 

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #11 on: October 16, 2019, 06:37:57 pm »
Is your son really called "drop table"? ;)

If it is really homework, show this discussion, lay back and do nothing: you should have a maximum.......
(stupid questions - not yours -  need inventive answers)
« Last Edit: October 16, 2019, 08:02:29 pm by Thaddy »
also related to equus asinus.

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Swap variables
« Reply #12 on: October 16, 2019, 08:09:15 pm »
Got it?
Winni
Let's hope the teacher got it...  O:-)
also related to equus asinus.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7495
Re: Swap variables
« Reply #13 on: October 16, 2019, 09:16:03 pm »
A well written application has at least fields for name, prefix, suffix and surname, where prefix and suffix are optional.

Are Dutch so called tussenvoegsels still removed from sorting order?

Anyway, I suggest to just remove the thread?

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: Swap variables
« Reply #14 on: October 16, 2019, 09:16:43 pm »
[…] homework […]
Uhh, didn’t we agree, that if something easy smells like homework we show extra complicated answers, something a (novice) student wouldn’t necessarily come up with (and hence has troubles to explain)?

Thaddy recently demonstrated a usage case of TStringHelper, so I had a go with it, too:
Code: Pascal  [Select]
  1. {$mode objFPC}
  2. {$longStrings on}
  3. uses
  4.         sysUtils;
  5.  
  6. function swappedNames(fullName: string): string;
  7. var
  8.         names: TStringArray;
  9. begin
  10.         if fullName.countChar(' ') = 1 then
  11.         begin
  12.                 names := fullName.split(' ');
  13.                
  14.                 // the actual swap
  15.                 fullName := names[1];
  16.                 names[1] := names[0];
  17.                 names[0] := fullName;
  18.                
  19.                 swappedNames := fullName.join(' ', names);
  20.         end
  21.         else
  22.         begin
  23.                 swappedNames := TStringHelper.empty; // or just emptyStr
  24.         end;
  25. end;
« Last Edit: October 16, 2019, 09:29:10 pm by Kays »
Yours Sincerely
Kai Burghardt