1

I want to print the name code input by user

begin:-read(X),
      name(X,L),
      write('Name Code: '),
      beginStr(L).

beginStr([H|T]):-
                 
                 put(H),
                 beginStr(T).

beginStr([]):-nl.

The Name Code below is the output i want to get, what else should be added to my code

| ?- begin.
|: 'Jenny Liz Ane'.
Name Code: JLA
yes


EddieJn
  • 47
  • 2

3 Answers3

0

1- First the user enters his/her first/middle/last name.

2- It is read.

3- string_chars breaks the string into characters : peter will become p,e,t,e,r

4- getFirstLetter Predicate extracts the first element from the list: from peter we get p.

5- upcase_atom convert lowercase letters to uppercase: p will become P.

6- display the answer using write.

 k:- 
  write('Enter First name: '),nl, 
  read(FName),nl, 
  string_chars(FName,N1),
  getFirstLetter(N1,L1),
  upcase_atom(L1,Str1),
  
    
  write('Enter Middle name: '),nl, 
  read(MName),nl, 
  string_chars(MName,N2),
  getFirstLetter(N2,L2),
  upcase_atom(L2,Str2),
  
    
  write('Enter Last name: '),nl, 
  read(LName),nl, 
  string_chars(LName,N3),
  getFirstLetter(N3,L3),
  upcase_atom(L3,Str3),
  write(Str1),write(' '),write(Str2),write(' '),write(Str3).  

 getFirstLetter([H|_],H).

Example:

?-k.

Enter First name:
peter

Enter Middle name:
jane

Enter Last name:
mary

P J M
Reema Q Khan
  • 878
  • 1
  • 7
  • 20
  • Is there any other solution? Because i need to input the query in a single line – EddieJn Dec 26 '20 at 21:22
  • With read you can get 1 word only. If you're using win-prolog, have a look at this answer: https://stackoverflow.com/questions/25571116/read-string-and-single-out-every-word-in-the-string-in-win-prolog – Reema Q Khan Dec 26 '20 at 22:35
0

Ok, I have two different approaches therefore 2 answers.
First approach: print the first character and each character after a space:

beginStrSpace(Name):-
    string_chars(Name,[H|T]),
    write(H),
    spaces(T).

spaces([]):-
    nl.
spaces([' ',H|T]):-
    !,
    write(H),
    spaces(T).
spaces([_|T]):-
    spaces(T).

?- beginStrSpace("Hello there").
Ht
true.

The idea is to split the strings into a list of characters and print the first one. The character list will then be proceeded. If the current head of the list is a space (' ') then the following character is printed, the rest is proceeded. If the first character is not a space, the predicate just seaches in the rest list until eventually the list is empty.

DuDa
  • 3,718
  • 4
  • 16
  • 36
  • Hi ,may i ask is this code in win-prolog?Because when i enter beginStrSpace("Hello there") , it shows Type Error and strbyt([72,101,108,108,111,32,116,104,101,114,101],_51908). – EddieJn Dec 27 '20 at 22:23
  • Sorry I can't test in Win Prolog, but it looks like something went wrong from converting decimal Ascii code to characters within string_chars/2... – DuDa Dec 27 '20 at 22:34
  • Is there anyone you know that might able to solve this prob?Im having a hard time doing this... – EddieJn Dec 27 '20 at 23:38
  • @EddieJn you have to find a way to split a string into its chars, the rest is a piece of cake. Try `string_chars("Hello", N).`, once by copying it and once by typing it by yourself. Try `string_chars('Hello', N).`. if any of these gives the right result, apply it to the code. – DuDa Dec 28 '20 at 10:55
  • @EddieJn you can also set the system preferences (`:- set_prolog_flag(double_quotes, chars).`) as suggested in [this](https://stackoverflow.com/a/65152480/8080648) post from [Isabelle Newbie](https://stackoverflow.com/users/4391743/isabelle-newbie). Works for SWI Prolog. – DuDa Dec 28 '20 at 15:52
0

My second approach would to be to search for capital letter

capital(C):-
    member(C, ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']).
   
beginStrCapital(Name):-
    string_chars(Name,L),
    capitals(L).

capitals([]):-
    nl.
capitals([H|T]):-
    (    capital(H)
    ->   write(H)
    ;    true
    ),
    capitals(T).

?- beginStrCapital("Hello There.").
HT
true.

The idea is to go through the list of letters and oif a letter is a capital, print it. I could not find the Character-Number conversion on the fly so I just am asking if a char is a member of the list of capitals. This program will not detect non-capital letters as in "Ludwig van Beethoven".

DuDa
  • 3,718
  • 4
  • 16
  • 36